#33 瘋狂購物 App — UIStepper 和數字變字串練習
Published in
Sep 6, 2021
作業目的: 學習 UIStepper 和將數字變成畫面上的文字。
Stepper最大最小值以及每次點選增加數量可以調整
點選Stepper,Qty跟著改變
ulongTeaCount = Int(ulongTeaStepper.value)lemonSodaCount = Int(lemonSodaStepper.value)mangoSodaCount = Int(mangoSodaStepper.value)lemonTeaCount = Int(lemonTeaStepper.value)ulongTeaQtyLabel.text = String(ulongTeaCount)lemonSodaQtyLabel.text = String(lemonSodaCount)mangoSodaQtyLabel.text = String(mangoSodaCount)lemonTeaQtyLabel.text = String(lemonTeaCount)
計算方式
//字串轉數字let ulongTeaPrice = Float(ulongTeaPriceLabel.text!)!let lemonSodaPrice = Float(lemonSodaPriceLabel.text!)!let mangoSodaPrice = Float(mangoSodaPriceLabel.text!)!let lemonTeaPrice = Float(lemonTeaPriceLabel.text!)!let sum = ulongTeaPrice * Float(ulongTeaCount) + lemonSodaPrice * Float(lemonSodaCount) + mangoSodaPrice * Float(mangoSodaCount) + lemonTeaPrice * Float(lemonTeaCount)//利用 NumberFormatter 顯示 Moneylet formatter = NumberFormatter()formatter.numberStyle = .currency//數字轉字串let moneyString = formatter.string(from: NSNumber(value: sum))totalPriceLabel.text = "\(moneyString!)"
完整程式碼:
GitHub
・參考資料