标准的swift's round()函数只会切断所有内容,并在该点之后留下1个位置。
答案 0 :(得分:0)
extension Double {
// Rounds the double to decimal places value
// Usage: (doubleVar).roundToPlaces(x)
// Where x is the number of places
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
}
来源:Rounding a double value to x number of decimal places in swift