Swift version: 5.2
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 From January 26th to 31st you can join a FREE crash course for iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a senior developer!
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.