Last active
October 10, 2017 17:27
-
-
Save khawajafarooq/73b357861319a5ee86a71f1b602b3773 to your computer and use it in GitHub Desktop.
Rounding a double number to a number of decimal places
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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