{

# Multiple ways to parse to absolute number swift with example

This tutorial explains how to calculate the absolute value of a number in swift programming.

The absolute value of a number returns a number by removing the negative sign.

For example, The input number is -123, And the absolute value is 123. if the number is 11 and the absolute value is 11.

There are multiple ways to convert to absolute value,

• One way is using the Int magnitude property and
• Another way using `abs` for Int, `fabs` for double, and `fabsf` for float types

## Swift Convert to the absolute value

First Import the `foundation` into code to use these methods

• abs() function for Int type

It returns the absolute value of a number.

``````import Foundation;

var number = -123
print(abs(number)) //123
number=1231
print(abs(number)) //1231
``````
• fabs() function for Double type

It returns the absolute value of a number.

``````import Foundation;

var number :Double = -123.123
print(fabs(number)) //123.123
``````
• fabsf() function for Float type fabsf() function accepts Float type and return absolute value.
``````import Foundation;

var number :Float = -123.123
print(fabsf(number))
``````
• use Int magnitude property of Int type
``````import Foundation;

var number  = -345.123
print(number.magnitude) //345.123
``````

## Conclusion

There are multiple ways we can convert to absolute value for a given number. The number can be Int, Float, or Double type

THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts