Swift
absolute value
programming
Swift tutorial
coding basics

Swift - Convert to absolute value

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Swift, the normal way to convert a signed number to its absolute value is abs(...). It works for common numeric types and is the direct, readable answer in most code.

The one edge case worth remembering is integer overflow for the minimum value of a signed integer type, such as Int.min. That case is rare, but it explains most surprises around abs.

Basic Usage

For everyday values:

swift
let a = -10
let b = abs(a)
print(b)   // 10

It also works with floating-point values:

swift
let x = -3.14
let y = abs(x)
print(y)   // 3.14

This is the standard solution. You do not need a custom helper for normal cases.

Works Across Signed Numeric Types

You can use abs with Int, Double, Float, and other signed numeric types:

swift
1let i: Int = -42
2let d: Double = -42.5
3let f: Float = -2.75
4
5print(abs(i))
6print(abs(d))
7print(abs(f))

Swift resolves the correct overload based on the type of the value you pass in.

A Reusable Helper

If you want to wrap the idea in your own utility, keep it simple:

swift
1func magnitude(_ value: Double) -> Double {
2    abs(value)
3}
4
5print(magnitude(-8.2))

Usually this is unnecessary, but sometimes it helps when domain language such as "distance" or "magnitude" reads better in your code.

Absolute Value in Real Code

A common real use is distance along one axis:

swift
1let oldX = 120
2let newX = 95
3let moved = abs(newX - oldX)
4print(moved)   // 25

Or checking whether a numeric error is within a tolerance:

swift
1let expected = 100.0
2let actual = 99.6
3let error = abs(expected - actual)
4
5if error < 1.0 {
6    print("Close enough")
7}

This is where abs is genuinely more expressive than a manual if value < 0 branch.

The Important Edge Case: Int.min

This is the one case developers should know:

swift
let value = Int.min
// let result = abs(value)

Why is it problematic? Because the positive version of Int.min cannot be represented in the same signed integer range. There is one more negative value than positive value in two's-complement integer types.

So if that case is possible in your data, guard it explicitly:

swift
1let value = Int.min
2
3if value == Int.min {
4    print("Cannot take abs safely as Int")
5} else {
6    print(abs(value))
7}

If you truly need to handle that extreme value, you may need a wider type or a different representation strategy.

abs Versus Manual Branching

This:

swift
let result = abs(number)

is generally better than:

swift
let result = number < 0 ? -number : number

because it is clearer and less error-prone. The standard library already expresses the intent directly.

Common Pitfalls

The most common pitfall is forgetting about Int.min. For most numbers abs is trivial, but that one value cannot be negated safely in the same type.

Another mistake is writing custom absolute-value helpers for common types when Swift already provides the standard function.

People also mix up absolute value with rounding. abs(-3.7) is 3.7, not 4.

Finally, remember that abs removes the sign. If your code actually needs direction information, taking the absolute value may hide data you still need later.

Summary

  • Use abs(...) to convert a signed number to its absolute value in Swift.
  • It works for common integer and floating-point types.
  • It is clearer than hand-written sign checks for normal code.
  • Be careful with Int.min, because its absolute value cannot be represented as an Int.
  • Use absolute values when you want magnitude, not when you still need direction.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.