I am using the keyboard type: UIKeyboardTypeDecimalPad for two UITextField objects.
When trying to perform an addition, I get different results depending on the current locale:
Case 1: US Format: the decimal point appears as . as expected. If I add 12.3 (text field 1) + 12.3 (text field 2), the answer will be 24.6. That’s what I want. But:
Case 2: Egypt Format: the decimal point appears as ,. If I repeat the same calculation the answer will be 24. The decimal portion is ignored. Is there a fix for that?
Note: I used the [textField.text floatValue] method.
Because
floatValuedoes non-localized scanning, it expects a “US format”.You can use an NSScanner object for localized scanning of numeric values from a string.
See: String Programming Guide: Scanners
Note the last paragraph:
See also: How to do string conversions in Objective-C?
Example: