简体   繁体   English

双精度损失

[英]Double precision lost

I m storing some values into the sqlite database. 我将一些值存储到sqlite数据库中。 So the data is collected from a textfield as a string and then converted to double and persisted. 因此,数据是作为字符串从文本字段中收集的,然后转换为double并保持不变。

This is something which I tried 这是我尝试过的

NSDecimalNumber answer;
value.answer = [NSDecimalNumber decimalNumberWithString:answerValue];

If you use 6532.002 as the data, it loses its precision. 如果您使用6532.002作为数据,它将失去其精度。

Any idea how to solve this issue. 任何想法如何解决这个问题。 I understand that similar issue has been posted numerous times but after trying a number of recommended solutions I have not been able to solve the issue yet. 我了解类似的问题已经发布了很多次,但是尝试了许多推荐的解决方案后,我仍然无法解决该问题。

Thanks 谢谢

Your number can't be represented exactly as a double precision floating point value due to the way floating point numbers work. 由于浮点数的工作方式,您的数字不能完全表示为双精度浮点值。 You'll need to find another way to represent it if you need that sort of precision. 如果需要这种精度,则需要找到另一种表示方式。 Some options are to use an arbitrary precision library (such as the ones listed in this Wikipedia entry ), or to use a fixed point representation with the number of bits you need to obtain the precision you need. 一些选择是使用任意精度库(例如Wikipedia条目中列出的库),或者使用具有所需位数的定点表示形式以获得所需的精度。 If you're only displaying the numbers, then you should keep it as a string. 如果仅显示数字,则应将其保留为字符串。

采用

float value=[textField.text floatValue];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM