0

Problem: A textField with a "number" containing a decimal separator (a comma in my case), how can I change this to a decimal separator that Xcode will understand (.), and the other way around - display a result with the local decimal separator?

Ok...half way there...

let label = Input.text
    let formatter = NumberFormatter()
    let maybeNumber = formatter.number(from: label!)
    if maybeNumber != nil {
        Output.text = String(describing: maybeNumber!)
        }
  • Have you looked into `NumberFormatter`'s `decimalSeparator` property? You can set it to a (.) or (,) if that helps. Any more details or code samples might help us figure out exactly what you're trying to do. – M-P Nov 02 '16 at 18:41
  • http://stackoverflow.com/a/30154625/2303865 – Leo Dabus Nov 03 '16 at 00:54
  • Ok....half way there...let label = Input.text let formatter = NumberFormatter() let maybeNumber = formatter.number(from: label!) if maybeNumber != nil { Output.text = String(describing: maybeNumber!) } } – Tom Smellror Nov 03 '16 at 16:32

1 Answers1

0

I'm not sure if this is what you mean, but you can change the comma in the textField.text to a (.) by using this piece of code:

textField.text = textField.text.replacingOccurrences(of: ",", with: ".", options: NSString.CompareOptions.literal, range: nil) 

This finds the commas in your textField and replaces them with (.)

Morten
  • 5
  • 5
  • Thank you Michael and Morten; What I am trying to do is having a decimal number input with the Local Decimal Pad ("22,4"). Then change this to 22.4 for further calculations.....then again display the result in a Local decimal format. – Tom Smellror Nov 03 '16 at 08:07