I'm trying to make my numbers (taken from custom button input) to move from right to left with two decimal places. I'm using 10 buttons (0-9) with sender tags attached to them for the buttonInput
and I want the right to left decimal number to appear in the labelText
label. I know you can use textfields to accomplish this, but I want to use my custom buttons.
Desired outcome:
user taps 1 button, labelText
changes to $0.01;
user taps 2 button, labelText
changes to $0.12; etc..
Xcode Version 12.1; Swift Version 5.3
Here's what I've got so far.. I'm new to Swift, so go easy on me!
@IBOutlet weak var labelText: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
@IBAction func numPad(_ sender: UIButton) {
//button number
let buttonInput = Double(sender.tag - 1)
//label text to number conversion
let labelValue = labelText.text
let intValue = Double(labelValue!)
//move number to the right of decimal
//let addedValue = (intValue! * 0.10) + buttonInput
let addedValue = intValue! + buttonInput
let numForm = NumberFormatter()
numForm.numberStyle = .decimal
//numForm.currencySymbol = "$"
numForm.minimumIntegerDigits = 0
numForm.maximumIntegerDigits = 4
numForm.maximumFractionDigits = 2
numForm.minimumFractionDigits = 2
labelText.text = numForm.string(for: addedValue)
}