开发者

problem when get floatValue from NSString

开发者 https://www.devze.com 2023-01-26 19:57 出处:网络
I am confusing and don\'t know why it\'s happen that when I get float from string. I have an array of NSString, and when I get floatValue from these string object, they are not correct, for an exampl

I am confusing and don't know why it's happen that when I get float from string.

I have an array of NSString, and when I get floatValue from these string object, they are not correct, for an example:

NSString object is "71.4" => floatValue is 71.4000013

NSStrin开发者_如何学编程g obtect is "71.45"=> floatValue is 71.4489839

Does anyone know the root cause and how to resolve, please help me!

Thanks!


Welcome to the joys of floating point. These ARE 'correct'. Floating point values are very inaccurate due to how they're stored on the system.

Here is a much better explanation than I'll be able to give.


Try doubleValue.

0

精彩评论

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