Swift 3迁移 - 双重延伸四舍五入问题 [英] Swift 3 Migration - Double Extension rounding issue
问题描述
我有一个 Double
扩展名中的方法,将 Double
到一定数量的数字:
public func roundToPlaces(places:Int) - >双{
let divisor = pow(10.0,Double(places))
return round(self * divisor)/ divisor
}
例如:
12.34567.roundToPlaces(2)
应该返回 12.35
。但是,我收到此扩展中使用的轮
方法的编译问题。这是说我不能使用mutation成员不可变值:'self'是不可变的
。
有关这里发生了什么的想法?如何解决这个问题?
我已经解决了这个问题。将 round(self * deisor)
更改为(self * divisor).rounded()
解决了编译问题。 p>
I'm migrating our codebase to Swift 3 and I've come across a compilation issue that I can't explain or fix.
I have a method in a Double
extension that rounds the Double
to a certain number of digits:
public func roundToPlaces(places: Int) -> Double {
let divisor = pow(10.0, Double(places))
return round(self * divisor) / divisor
}
For example:
12.34567.roundToPlaces(2)
should return 12.35
. However, I'm getting a compilation issue for the round
method used in this extension. It's saying that I Cannot use mutating member on immutable value: 'self' is immutable
.
Any ideas on what's going on here? How do I fix this issue?
I've fixed the issue. Changing round(self * divisor)
to (self * divisor).rounded()
resolved the compilation issue.
这篇关于Swift 3迁移 - 双重延伸四舍五入问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!