NSString 转换 float 的精度问题, 换double类型可以解决
@”0.01” 转换成float时, 经常会变成 0.009999799 这种形式, 因为float类型无法精准保存, 系统会选一个接近的值来代替.
而double类型则可以有更好的精度.

http://stackoverflow.com/questions/9328260/converting-nsstring-to-float-adds-in-decimal-places
Your issue seems to be that you don’t understand how floats are stored in memory and don’t know that floats aren’t precise.
Exact values often can’t be stored and so the system picks the closest number it can to represent it. If you look carefully, you can see that each of the outputted numbers is very close to your inputted values.
For better accuracy, try using double instead. Double does encounter the same problems, but with better

当使用NSString转换为float时,可能会遇到精度丢失的问题,如"0.01"转为0.009999799。这是因为float类型无法精确表示某些数值。为了解决这个问题,可以改用double类型,它提供了更高的精度。
最低0.47元/天 解锁文章
1336

被折叠的 条评论
为什么被折叠?



