Poles Poles - 18 days ago 24
iOS Question

Getting error while rounding a double value with 3 digits precision in swift 3

I'm using this extension to round a double value with 3 digits precision.

extension Double {
var DigitsPrecision: String {

return String(Double(round(1000*self)/1000)) //round the double with 3 digits precision
}
}


It worked perfectly on swift 2.2 but when I migrated my project to swift 3.0 its throws this error

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


How to solve this?

Answer

I suggest you use the new rounded method on the value itself:

extension Double {
    var digitsPrecision: String {
        return String(Double((1000 * self).rounded() / 1000))
    }
}
Comments