How to convert a number to an absolute value in Swift?


To convert a number to its absolute value in Swift, you can use the abs(_:) function. In this article, you will see multiple examples of how to use the abs() function in the Swift language.

Example 1

In this example, you will convert a simple negative number to an absolute value.

  • Step 1 − Declare an input variable with an initial numeric value

  • Step 2 − Convert the numeric input value into absolute value using the abs() function

  • Step 3 − Assign the output absolute value to a new variable

  • Step 4 − Print the input value to the console

  • Step 5 − Print the output value to the console

let numberValue = -10
let absoluteValue = abs(numberValue)
print("Original value: \(numberValue)")
print("Absolute value: \(absoluteValue)")

Output

Original value: -10
Absolute value: 10

Example 2

In this example, you will see how we can convert an integer type of number to an absolute value.

  • Step 1 − Declare an input variable with an initial integer value

  • Step 2 − Convert the numeric integer value into absolute value using the abs() function

  • Step 3 − Assign the output absolute value to a new variable

  • Step 4 − Print the input value to the console

  • Step 5 − Print the output value to the console

let integerValue = -10
let absoluteIntegerValue = abs(integerValue)
print("Original value: \(integerValue)")
print("Absolute value: \(absoluteIntegerValue)")

Output

Original value: -10
Absolute value: 10

Example 3

In this example, you will see how we can convert a double type of number to an absolute value

  • Step 1 − Declare an input variable with an initial numeric value

  • Step 2 − Convert the numeric input value into absolute value using the abs() function

  • Step 3 − Assign the output absolute value to a new variable

  • Step 4 − Print the input value to the console

  • Step 5 − Print the output value to the console

let doubleValue = -10.5
let absoluteDoubleValue = abs(doubleValue)
print("Original value: \(doubleValue)")
print("Absolute value: \(absoluteDoubleValue)")

Output

Original value: -10.5
Absolute value: 10.5

Example 4

In this example, you will see how we can convert a zero number to an absolute value.

  • Step 1 − Declare an input variable with an initial numeric value

  • Step 2 − Convert the numeric input value into absolute value using the abs() function

  • Step 3 − Assign the output absolute value to a new variable

  • Step 4 − Print the input value to the console

  • Step 5 − Print the output value to the console

let zeroValue = 0
let absoluteZeroValue = abs(zeroValue)
print("Original value: \(zeroValue)")
print("Absolute value: \(absoluteZeroValue)")

Output

Original value: 0
Absolute value: 0

Example 5

In this example, you will see how to perform arithmetic operations with different types of numbers.

  • Step 1 − Declare an input variable with an initial negative value

  • Step 2 − Declare another variable with an initial integer value

  • Step 3 − Declare another variable with an initial double value

  • Step 4 − Convert the output generated by an arithmetic operation between negative and positive values.

  • Step 5 − Assign the output absolute value to a new variable i.e. result1

  • Step 6 − Convert the output generated by an arithmetic operation between negative and double values.

  • Step 7 − Print the first result value to the console

  • Step 8 − Print the second result value to the console

let negativeValue = -10
let integerValue = 5
let doubleValue = 2.5
let result1 = abs(Double(negativeValue) / Double(integerValue))
let result2 = Double(abs(negativeValue)) * doubleValue
print(result1)
print(result2)

Output

2.0
25.0

Example 6

In this example, you will see how to perform arithmetic operations with different types of numbers

  • Step 1 − Declare an input variable with an initial negative value

  • Step 2 − Declare another variable with an initial integer value

  • Step 3 − Declare another variable with an initial integer value

  • Step 4 − Convert the output generated by an arithmetic operation between negative and first integer values.

  • Step 5 − Assign the output absolute difference value to a new variable i.e. absoluteDifference

  • Step 6 − Convert the output generated by an arithmetic operation between the negative and both the integer values by multiplying them.

  • Step 7 − Print the first result value (absolute difference) to the console

  • Step 8 − Print the second result value (product of absolute values) to the console

let negativeValue = -7
let integerValue = 3
let integerValue2 = 4
let absoluteDifference = abs(negativeValue - integerValue)
let productOfAbsoluteValues = abs(negativeValue) * abs(integerValue) * abs(integerValue2)
print(absoluteDifference)
print(productOfAbsoluteValues)

Output

10
84

Example 7

In this example, we will filter positive numbers from an array that contains positive and negative numbers.

  • Step 1 − In this example, we start by declaring an array called numberArray with several integers, some of which are negative.

  • Step 2 − We then use the map(_:) function to apply the abs(_:) function to each element in numberArray. This converts all the negative numbers to their absolute values while leaving the positive numbers unchanged.

  • Step 3 − Finally, we use the filter(_:) function to remove any 0s from the resulting array and return only the positive absolute values.

let numberArray = [-2, 5, -8, 9, 0, -3, 6]
let positiveNumberArray = numberArray.map(abs).filter { $0 > 0 }
print("Number Array: \(numberArray)")
print("Positive Number Array: \(positiveNumberArray)")

Output

Number Array: [-2, 5, -8, 9, 0, -3, 6]
Positive Number Array: [2, 5, 8, 9, 3, 6]

The final output is an array called positiveNumberArray with only the positive absolute values from numberArray. This can be useful for various purposes, such as calculating the sum or average of only the positive values in an array.

Conclusion

Swift provides an in-built function called abs() to convert numeric values to absolute values. This function belongs to the math library in the Foundation framework. You can use this function for any numeric value like integer, double, float, etc. This function always returns a positive or zero value. The abs() function may not be efficient for large numbers because it requires an operation to be performed every time it is called.

Updated on: 04-Apr-2023

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements