我有以下扩展名
extension Double {
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded / divisor
}
}
Run Code Online (Sandbox Code Playgroud)
当我将它更新到Swift 3时,它不起作用.我在这里尝试了解决方案,但我得到了
Binary operator '/' cannot be applied to operands of type '_' and 'Double' 错误
你忘了一对括号:应该rounded()代替rounded:
extension Double {
func roundToPlaces(places:Int) -> Double {
let divisor = pow(10.0, Double(places))
return (self * divisor).rounded() / divisor
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
770 次 |
| 最近记录: |