1

我将一些值存储到 sqlite 数据库中。因此,数据作为字符串从文本字段中收集,然后转换为双精度并持久化。

这是我尝试过的东西

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

如果您使用 6532.002 作为数据,它会失去精度。

任何想法如何解决这个问题。我知道类似的问题已经发布了很多次,但是在尝试了一些推荐的解决方案之后,我还没有解决这个问题。

谢谢

4

2 回答 2

1

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. If you're only displaying the numbers, then you should keep it as a string.

于 2013-03-05T05:44:17.250 回答
0

use

float value=[textField.text floatValue];
于 2013-03-05T11:36:58.857 回答