NSString到双重问题 [英] NSString to Double Issue

查看:244
本文介绍了NSString到双重问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能很简单,但我不明白......
我有一个NSString 50.81114 我希望将它转换为双... < br>
目前我正在使用 [string doubleValue] ,但这是 50.811140000002

发生了什么事?!

Probably really simple but I don't understand... I have an NSString 50.81114 and I want to convert it into a double...
Currently I am using [string doubleValue] but this is coming out as 50.811140000002
What's going on?!

Disco

推荐答案

Double (以及任何浮点数)都有自己的准确度限制,通常是15-16位左右。请注意,这不仅适用于Objective-C,而且适用于所有语言,因为二进制浮点显示的限制。

Double (and any floating point number) has its own limit of accuracy, normally it would be around 15-16 digits. Note this is not just for Objective-C, but for all the languages because of the limit of binary floating point presentation.

您显示的内容是正常的,因为 50.81114 无法以二进制形式准确显示,必须使用近似值。

What you show is just normal, since 50.81114 cannot be accurately presented in binary, approximation must be used.

您可以阅读维基百科供进一步阅读。

You can read Wikipedia for further reading.

这篇关于NSString到双重问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆