【发布时间】:2017-05-24 08:27:31
【问题描述】:
我是 Swift 编程的新手,我一直在 Xcode 8.2 中创建一个简单的小费计算器应用程序,我在下面的IBAction 中设置了我的计算。但是当我实际运行我的应用程序并输入一个要计算的数量(例如 23.45)时,它会出现超过 2 个小数位。在这种情况下如何将其格式化为.currency?
@IBAction func calculateButtonTapped(_ sender: Any) {
var tipPercentage: Double {
if tipAmountSegmentedControl.selectedSegmentIndex == 0 {
return 0.05
} else if tipAmountSegmentedControl.selectedSegmentIndex == 1 {
return 0.10
} else {
return 0.2
}
}
let billAmount: Double? = Double(userInputTextField.text!)
if let billAmount = billAmount {
let tipAmount = billAmount * tipPercentage
let totalBillAmount = billAmount + tipAmount
tipAmountLabel.text = "Tip Amount: $\(tipAmount)"
totalBillAmountLabel.text = "Total Bill Amount: $\(totalBillAmount)"
}
}
【问题讨论】:
标签: ios swift formatting currency