如何用数学轮将两倍迅速加倍到2位

时间:2016-02-22 10:09:00

标签: swift

标准的swift's round()函数只会切断所有内容,并在该点之后留下1个位置。

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