tags:

views:

63

answers:

2

I have a UITextField with number pad keyboard and on Editing Changed event I do this:

textField.text = [@"" stringByAppendingFormat:@"%0.2f", [[[textField text] stringByReplacingOccurrencesOfString:@"." withString:@""] floatValue]/100.0];

This is a script found on a site and it works just fine for the first 11 characters, meaning that if I type 10000000000 it displays correctly: 100000000.00

The problem come from the 12th character.
If I press again 0, it displays 999999979.52
And again 0 in shows 9999999959.04

Is it something related with the float size?

+1  A: 

You should avoid using floatValue of a string for display purposes, maybe doubleValue will work better for you.

Ben
+1  A: 

At the very least, you're dividing a floatValue by a double- either try doubleValue (as Ben suggests) or dividing by 100.0f.

kevboh