In my iPhone app I need the user to be able to enter a decimal amount in a textfield and then I need to convert it into float.
I use the following code:
CGFloat amount = [amountTextField.text floatValue];
It works fine when the user is from US and use point for decimal values, but for European users that use comma instead it doesn’t get the value. Should I just replace , with . before using floatValue or is there a better way?
a better way would be to use NSDecimalNumber, floats are ugly anyway. Because 2.2 should be 2.2 and not 2.2000000477
If you don’t want to use NSDecimalNumber have a look into NSNumberFormatter and NSNumber. You could convert it back to CGFloat (which is just another name for float) too.
When it comes to localization you always want to use the built-in functionality. When you use string editing you are doing it wrong.