Skip to content

Instantly share code, notes, and snippets.

@khawajafarooq
Last active October 10, 2017 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khawajafarooq/73b357861319a5ee86a71f1b602b3773 to your computer and use it in GitHub Desktop.
Save khawajafarooq/73b357861319a5ee86a71f1b602b3773 to your computer and use it in GitHub Desktop.
Rounding a double number to a number of decimal places
import Foundation
extension Double {
mutating func round(to places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return Darwin.round(self * divisor) / divisor
}
}
var decimal = 10035.11459
print(decimal.round(to: 2))
print(decimal.round(to: 3))
print(decimal.round(to: 4))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment