Swift version: 5.4
abs() function returns the absolute value of a number, which is a way of describing how far away from zero it is without thinking about whether it’s positive or negative. It’s most commonly used if you have a number that you need to be positive, because whether you pass 30 or -30 to
abs() you get back 30.
In code it’s just this:
let number = -15 let absolute = abs(number)
That will set
absolute to 15.
SPONSORED With Sentry’s error and performance monitoring for iOS, you see mobile vitals that actually matter, can solve any latency issues quickly, and learn how each release is performing over time.
Available from iOS 8.0
This is part of the Swift Knowledge Base, a free, searchable collection of solutions for common iOS questions.
Link copied to your pasteboard.