我是Swift編程的新手,我一直在Xcode 8.2中創建一個簡單的小費計算器應用程序,我在下面的IBAction
中設置了我的計算。但是,當我真正運行我的應用程序並輸入一個數量來計算(如23.45)時,它會出現超過2個小數位。在這種情況下,我如何格式化爲.currency
?如何將一個Double格式化爲貨幣 - Swift 3
@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)"
}
}
僅供參考 - 無需將格式化程序的「locale」設置爲「Locale.current」,因爲這是默認設置。 – rmaddy
是的,我知道,我添加了一條評論,說它是多餘的。想保持在那裏,以顯示它可以很容易地改變。 –
工作得很好。 –