Why this formula works just for 11 characters?
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 corr开发者_JAVA技巧ectly: 100000000.00
The problem come from the 12th character.
If I press again 0, it displays 999999979.52 And again 0 in shows 9999999959.04Is it something related with the float size?
At the very least, you're dividing a floatValue
by a double- either try doubleValue
(as Ben suggests) or dividing by 100.0f
.
You should avoid using floatValue of a string for display purposes, maybe doubleValue will work better for you.
精彩评论