在斯威夫特中获得两倍

时间:2016-05-20 21:48:12

标签: swift double rounding

我正在进行计算以找到double(下面)

的值
let tipAmt = Double(billAmt! * tipPer)

但是,我想取这个值并将其四舍五入到最接近的整数。我该怎么做是否有一个圆形的电话?

1 个答案:

答案 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
相关问题