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 Join a FREE crash course for iOS devs who want to become complete senior developers — from October 18th to 24th. Learn how to apply iOS app architecture patterns through a series of lectures and practical coding sessions.
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.