Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/120.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在swift 3中以3位精度舍入双精度值时出错_Ios_Double_Swift3 - Fatal编程技术网

Ios 在swift 3中以3位精度舍入双精度值时出错

Ios 在swift 3中以3位精度舍入双精度值时出错,ios,double,swift3,Ios,Double,Swift3,我用这个扩展来舍入一个精度为3位的双精度值 extension Double { var DigitsPrecision: String { return String(Double(round(1000*self)/1000)) //round the double with 3 digits precision } } 它在swift 2.2上运行得非常好,但是当我将我的项目迁移到swift 3.0时,它抛出了这个错误 Cannot use mutatin

我用这个扩展来舍入一个精度为3位的双精度值

extension Double {
    var DigitsPrecision: String {

        return String(Double(round(1000*self)/1000)) //round the double with 3 digits precision
    }
}
它在swift 2.2上运行得非常好,但是当我将我的项目迁移到swift 3.0时,它抛出了这个错误

Cannot use mutating member on immutable value: 'self' is immutable

如何解决此问题?

我建议您对值本身使用新的
舍入方法:

extension Double {
    var digitsPrecision: String {
        return String(Double((1000 * self).rounded() / 1000))
    }
}
可能重复的