1

I have 2 functions that are converting string (decimal numbers passed as string) localized numbers, and vice versa. Here is the code:

static func localizedNumberForText(text: String) -> String {
    let num = Double(text)
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.maximumFractionDigits = 30;

    if num != nil {
        return  formatter.string(from: num! as NSNumber)!
    } else {
        return text
    }
}

static func stringFromLocalizedNumber(text: String) -> String {
    let formatter = NumberFormatter()
    formatter.numberStyle = .decimal
    formatter.maximumFractionDigits = 30;
    let num = formatter.number(from: text)

    if let number = num {
        return String(describing: number)
    } else {
        return text
    }
}

This works perfect with static values, but I have a challenge that while UITextField is being edited I need to format localized string in a different way. This is the example that works: - I have number 123456789 and it is localized fine with thousand separator at display time as following: 123.456.789

But when user inside UITextField deletes last character from this string it becomes 123.456.78 and in function stringFromLocalizedNumber fails to get num from text because formatted text is not correctly localized. Any idea how I can fix this issue?

What I want to achieve is, after user deletes a numeric character or adds one to remove localization that is incorrect and apply new one that correctly parses this string as a number.

Aleksandar
  • 1,457
  • 13
  • 30
  • http://stackoverflow.com/a/29783546/2303865 – Leo Dabus Mar 29 '17 at 11:33
  • 1
    BTW no need to cast to NSNumber just use `string(for:)`instead of `string(from:)` – Leo Dabus Mar 29 '17 at 11:34
  • Don't let people edit formatted numbers. When the field begins editation, change the text to unformatted (remove grouping separators) and add them again when the field loses focus. That's the most robust solution. – Sulthan Mar 29 '17 at 11:39
  • Hmmm this suggestion @Sulthan makes things a lot easier. I will apply that logic then. To have normalized numbers when I begin editing value, and put back the localization as soon as editing is done. – Aleksandar Mar 29 '17 at 12:10
  • the link above does it all and is also works with currencies that doesn't have fraction digits – Leo Dabus Mar 29 '17 at 12:45
  • @Leo That does work with currency, but I need it as you can see it from the code to work with `.decimal`. @Sulthan what I have now is the following: When I strip localization and display only number, number is displayed with dot as decimal separator. And my localization to Danish displays decimal keyboard with comma as a separator. The separator displayed should be exactly the same as the one from the keyboard. How do I do that? – Aleksandar Mar 30 '17 at 07:25
  • Found a solution with "stripping off" grouping separator, and replacing occurrences of comma with dot that covers decimal separator for different localizations. Then conversion into number always works. – Aleksandar Mar 30 '17 at 08:17

0 Answers0