Swift version: 5.1
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 Catch bugs as soon as they happen and know exactly why a crash occurred by integrating Instabug's SDK in one minute. You will automatically receive device data, network logs, and reproduction steps with every bug and crash report.
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.