2016-12-24 59 views
1

的地方,我有以下扩展扩展双轮斯威夫特3

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

当我将其更新到斯威夫特3,这是行不通的。我试图解决here,但我得到了

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

回答

4

你忘了一个括号:应该是rounded()而不是rounded

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

哦,对了。万分感谢! –