ios - 如何将四舍五入的整数显示到标签?
问题描述
我正在尝试制作一个将 UILabel 中的整数四舍五入的函数。我有两个 totalLabel.text 方法,但是在声明了第二个之后,我发现总数现在刚刚四舍五入。我正在尝试使它显示原始的十进制数字,并且当按下按钮时,它会将总数向上舍入。代码:
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
@IBAction func onTap(_ sender: Any) {
view.endEditing(true)
}
@IBAction func updateTip(_ sender: Any) {
updateUI()
}
@IBAction func splitTotal(_ sender: UIStepper) {
updateUI()
}
@IBAction func roundUpButtonPressed(_ sender: UIButton) {
updateUI()
}
func updateUI() -> Void {
let tipPercentages = [0.10, 0.15, 0.20, 0.25]
let numPeople = stepper.value
let bill = Double(billField.text!) ?? 0
let tip = bill * tipPercentages[tipController.selectedSegmentIndex]
let total = bill + tip
let totalSplit = total / numPeople
let tipSplit = tip / numPeople
// roundUp declaration.
let roundUp = round(total)
tipLabel.text = String(format: "$%.2f", tip)
totalLabel.text = String(format: "$%.2f", total)
splitTotal.text = String(format: "$%.2f", totalSplit)
splitTip.text = String(format: "$%.2f", tipSplit)
// Display rounded total to total label.
totalLabel.text = String(format: "$%.2f", roundUp)
numberOfPeople.text = "\(Int(numPeople))"
}
}
解决方案
推荐阅读
- r - 计算 shortest.paths igraph 上的顶点数
- python-2.7 - 将二进制图像重塑为一维矢量 Python
- php - 如何检查多维数组键和值是否相同?
- c++ - C++ OpenCV 3.4:HOG detectMultiScale() / detect() 问题
- hibernate - 子模块中的 Spring Jpa 表
- javascript - innerHTML 不更新 DOM
- python - python特定频率去除(陷波滤波器)?
- caching - 内存缓存 GetOrCreate 和 MemoryCacheEntryOptions
- docker - 在 docker 容器中使用 nightwatch 进行测试
- java - 如何验证在所需的失败尝试后用户帐户是否在 Selenium 中被阻止