如何在swift中用数学圆将双精度四舍五入到2位

如何在swift中用数学圆将双精度四舍五入到2位,swift,Swift,标准swift的round()函数只是将所有内容截断,并在点后保留1个位置 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

标准swift的round()函数只是将所有内容截断,并在点后保留1个位置

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
    }
}
资料来源:

资料来源: