Swift - Convert to absolute value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
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:
It also works with floating-point values:
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 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:
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:
Or checking whether a numeric error is within a tolerance:
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:
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:
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:
is generally better than:
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 anInt. - Use absolute values when you want magnitude, not when you still need direction.
Related reading
- Swift - Convert to absolute value
- Swift - encode URL
- Swift - encode URL
- Swift - How creating custom viewForHeaderInSection, Using a XIB file?
- Swift - How to convert String to Double
- Swift - How to detect orientation changes
- Swift - How to detect orientation changes
- Swift - How to dismiss all of view controllers to go back to root
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.