扩展双轮到Swift 3

时间:2016-12-24 04:53:01

标签: swift

我有以下扩展名

extension Double {
    func roundToPlaces(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded / divisor
    }
}

当我将其更新为Swift 3时,它不起作用。我尝试了解决方案here,但我得到了

Binary operator '/' cannot be applied to operands of type '_' and 'Double'错误

1 个答案:

答案 0 :(得分:5)

您忘记了一对括号:应该是rounded()而不是rounded

extension Double {
    func roundToPlaces(places:Int) -> Double {
        let divisor = pow(10.0, Double(places))
        return (self * divisor).rounded() / divisor
    }
}