<分区>
我编写了一个简单的 swift 程序来显示运行电子设备的成本。该程序运行良好(有点笨拙 - 我是 swift 的新手!)但结果在小数点后显示了几个数字,所以我试图将它四舍五入到小数点后两位。我运气不好!我的代码是:
var pricePerkWh: Double = 13.426
var watts: Double = 5.0
var hours: Double = 730.0
var KW: Double = watts/1000
var kWh: Double = KW*hours
var penceMonth: Double = kWh*pricePerkWh
var poundMonth:Double = penceMonth/100
var cost = poundMonth.roundTo(places: 2)
print ("It will cost £\(cost) per month")
从我读到的here ,使用了 roundTo(places: 2)
但这导致了错误
error: Power Costs.playground:6:12: error: value of type 'Double' has no member 'roundTo'
var cost = poundMonth.roundTo(places: 2)
如有任何指点,我们将不胜感激!
谢谢