I want to make a text field with double number formatting as user is typing. I use this text field for currency input (without any currency symbols etc). Here's what I tried so far:
@IBAction func onEditingChanged(_ sender: UITextField) {
let validString = sender.text!.replacingOccurrences(of: " ", with: "")
let price = Double(validString)!
let formatter = NumberFormatter()
formatter.decimalSeparator = "."
formatter.groupingSeparator = " "
formatter.numberStyle = .decimal
if let formattedTipAmount = formatter.string(from: price as NSNumber) {
self.currencyTextField.text = formattedTipAmount
}
}
However no matter how I try I cannot put a dot in my string to make this number double and as a result I can't edit/type the fractional part.
Here's how I see it should work:
User input 1: 2132
Displayed value in textfield: 2 132.00
User Input 2: 2132.54 (the user set a point before the dot separator and typed 54 to replace 00)
Output: 2 132.54
etc