0
我写了一个简单的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)
任何指针将不胜感激!
感谢
Leo Dabus提供的链接确实表明我的问题是重复的。对于那些未来可能会遇到的问题,我可以使用'var cost:String = String(format:“%。2f”,poundMonth)'这提供了期望的结果。谢谢。 – sark