开发者

Why this formula works just for 11 characters?

开发者 https://www.devze.com 2023-01-19 19:04 出处:网络
I have a UITextField with number pad keyboard and on Editing Changed event I do this: textField.text = [@\"\" stringByAppendingFormat:@\"%0.2f\", [[[textField text] stringByReplacingOccurrencesOfStri

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.04

Is 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号