首页 > 代码库 > 保留X位小数(Double) swift

保留X位小数(Double) swift

代码如下,输入要保留的小数个数

 

extension Double {

    /// Rounds the double to decimal places value

    func roundTo(places:Int) -> Double {

        let divisor = pow(10.0, Double(places))

        return (self * divisor).rounded() / divisor

    }

}

保留X位小数(Double) swift