0

I'm trying to get conversion fro user entered money strings to NSDecimalNumber

var nsdecimalNumberFromUserInput: NSDecimalNumber? {
    let parsed = NSDecimalNumber(string: self, locale: Locale.current)
    if parsed == .notANumber {
        return nil
    }
    return parsed
}

UIKeyboardType == .numberPad in UITextField somehow displays either . or , depending on the device and ios version (12.4[.1] & 13 beta 8). All current locales set to Belarus.

What's a reliable way to parse money regardless of the curved balls .numberPad sends my way?

The decimal separator in the above mentioned locale is , (comma)

Thanks

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Anton Tropashko
  • 5,486
  • 5
  • 41
  • 66
  • 1
    Use `NumberFormatter` to parse user-entered decimal numbers. – rmaddy Sep 17 '19 at 17:05
  • https://stackoverflow.com/questions/29782982/how-to-input-currency-format-on-a-text-field-from-right-to-left-using-swift/29783546 – Leo Dabus Sep 17 '19 at 18:12
  • Is the question about the decimal separator that appears on the keyboard (which, AFAIK, doesn’t even show up with `.numberPad`, but rather only `.decimalPad`)? Or is the question how to convert the string in the text field to `NSDecimalNumber` (or perhaps preferable, `Decimal`)? By the way, the behavior of all of this depends upon the localization settings in your project as well as on the respective simulators... – Rob Sep 17 '19 at 18:43
  • Rob, it's both about the proper keyboard and about a string to [NS]Decimal[Number] conversion that can handle improper separators. – Anton Tropashko Sep 18 '19 at 06:12

0 Answers0