I have a textfield that should accept decimal numbers; it should accept numbers with a comma (6,7) or a point (6.7) irrespective of the user’s locale.
I use the following code:
NSNumberFormatter* formatter = [[NSNumberFormatter alloc] init];
NSNumber* number = [formatter numberFromString:txtScore.text];
NSDecimalNumber* decimalNumber = [[NSDecimalNumber alloc] initWithDouble:[number doubleValue]];
The problem is that if I use a comma in the textfield, for some locales I always get back 0. The same is true for the point, some locales can read it, but others always return 0.
So, how can I get this textfield to work with both commas and points, irrespective of the user’s locale?
I figured out a hack to solve this problem.
I now replace every occurrence of a comma with a point and always use the en_US locale that works with a point.
This works, but I wonder if there is perhaps a more natural solution.