我正在进行计算以找到double(下面)
的值let tipAmt = Double(billAmt! * tipPer)
但是,我想取这个值并将其四舍五入到最接近的整数。我该怎么做是否有一个圆形的电话?
答案 0 :(得分:3)
实际上有一个round()
方法适用于Double
let billAmt: Double? = 23.75
let tipPer: Double = 0.15
let tipAmt = Double(billAmt! * tipPer)
print("tipAmt: \(tipAmt)") // 3.5625
var rounded = round(tipAmt)
print("rounded to nearest dollar: \(rounded)") // 4
rounded = round(tipAmt * 100) / 100
print("rounded to nearest cent: \(rounded)") // 3.56