There is a text field
@IBOutlet weak var txt: UITextField!
When I tried to convert the value of txt into Double its worked fine in US region.
For example
let magicNumber = Double(txt.text ?? "0.0")
If I type 12 UITextField then 12 assigned on magicNumber
Then I changed the region to Germany.
let magicNumber = Double(txt.text ?? "0.0")
If I type 12,8 UITextField then the magicNumber is nil.
I need a correct double value like : 12.8
How could I achieve this? Please suggest to me the correct way.
CodePudding user response:
Careful handling of localized numeric strings
let inputString = "12,8"
let formatter = NumberFormatter()
formatter.locale = Locale.current // Locale(identifier: "de")
let number = formatter.number(from: inputString)
print(number) // 12.8