0

I search how to replace a comma separator by a dot in numberPad in UITextField with Swift 5 please.

I tried this but it didn't work.

let commaValue = textField.text!
let decimalValue = Double(commaValue.replacingOccurrences(of: ",", with: "."))
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • The code you posted has nothing to do with changing the decimal separator of a number pad keyboard. What is your actual question? Do you want to actually modify the number pad (which you really shouldn't do) or do you need help properly converting a localized number string into a `Double`? Please [edit] your question and clarify what you really need help with. And be sure you clearly explain what trouble you are having with the code you posted including showing a specific example of actual input and desired output. – rmaddy Aug 20 '19 at 19:52
  • Hi! I search how to force decimalPad (UIKeyboadType) to change comma separator (,) to dot (.) – Benjamin Tourin Aug 21 '19 at 10:26
  • I’m in France and my app use the French decimalPad, so the comma separator and not the dot as I search how to force my app to use dot and not comma separator. – Benjamin Tourin Aug 21 '19 at 10:28
  • Why would want to force your app to use the wrong decimal separator than the user expects? – rmaddy Aug 21 '19 at 13:52
  • Because otherwise bug. Calculations don't work with comma separator (,), it's why I want force my app to displays a dot (.). My decimalPad (i.stack.imgur.com/9WfCU.png) and what I want for my app (i.stack.imgur.com/hriYC.png). If you can help me of course. – Benjamin Tourin Aug 21 '19 at 18:50
  • The proper solution is to use NumberFormatter so you can parse the entered number. – rmaddy Aug 21 '19 at 19:31
  • Yes, I have read this about NumberFormatter, but the problem is to implement it with my UITextField... – Benjamin Tourin Aug 21 '19 at 20:49
  • Update your question with your attempt to use NumberFormatter. Using a text field versus any any string is no different. – rmaddy Aug 21 '19 at 23:34

1 Answers1

4

You could use this:

let commaValue = "2,3"
let replaced = String(commaValue.map {
$0 == "," ? "." : $0 })
George
  • 328
  • 2
  • 8