Monday, 19 August 2013

How to restrict number of fraction digits when parsing number from string?

How to restrict number of fraction digits when parsing number from string?

I want to restrict the number of fraction digits a user is allowed to
enter into a UITextField that only accepts (localized) numeric input.
Example with 4 fraction digits allowed:
Good: 10.123, 12345.2345
Bad: 0.12345, 6.54321
Right now, I'm using NSNumberFormatter's numberFromString: in the
UITextField delegate's
textField:shouldChangeCharactersInRange:replacementString: to determine
whether it's a legal numeric value.
Unfortunately, NSNumberFormatter seems to ignore maximumFractionDigits in
numberFromString:. In tests using getObjectValue:forString:range:error: I
had the same problem, and range also was the full length of the string
afterwards (unless I start entering letters; then range indicates only the
part of the string with digits).
How to best restrict the number of fraction digits in user input?

No comments:

Post a Comment