<分区>
我正在使用下面的代码,并试图让我的“输出值”在按下按钮时向上舍入到小数点后两位。
我在网上找到了各种不同的方法,并尝试将它们结合起来,但没有成功。有人能给我指出正确的方向吗?
如果计算结果为 164.98001,那么我希望答案显示为 164.99。
编辑 - 我不认为这是一个重复的问题,因为我的问题与四舍五入所有数字有关。
@IBAction func buttoncalc(_ sender: Any) {
total.isHidden = false
let firstValue = Double(text1.text!)
let secondValue = Double(text2.text!)
let thirdValue = Double(text3.text!)
let forthValue = Double(text4.text!)
if firstValue != nil && secondValue != nil && thirdValue != nil && forthValue != nil {
let outputvalue = Double(((firstValue! * secondValue!)/1000)*0.5)*(thirdValue! - forthValue!)
total.text = "£ \(outputvalue)"
} else {
total.text = nil
}
}