You can tidy this up even more by making it an extension of Double
:
extension Double {
func truncate(places : Int)-> Double {
return Double(floor(pow(10.0, Double(places)) * self)/pow(10.0, Double(places)))
}
}
You use it like this:
var num = 1.23456789
// return the number truncated to 2 places
print(num.truncate(places: 2))
// return the number truncated to 6 places
print(num.truncate(places: 6))
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…