如何在iOS中获得高精度的值

How to obtain the high accuracy of value in iOS

服务器对我的响应是 'json object' :

the json:
{ wareAmount:"13.40" } ;

我的 iOS 代码是获取此 json 值并推入一个块:

__weak typeof(self) weakSelf = self ;
[taskAction invoke:(NSDictionary *result){
    weakSelf.wareAmount = [[result objectForKey:@"wareAmount"] doubleValue];
    // the weakSelf.wareAmount's value is 13.399936 ...
}] ;

为什么wareAmount(wareAmount是自己的属性对象)的值是13.39,而不是13.40?

这是因为计算机存储浮点值的方式。这是一个很大的主题,我建议您多读一些(可能从 here 开始)。最重要的是,在计算机中无法精确表示十进制数。

为了以某种方式缓解您的问题,我建议将您的数字存储为 NSNumber 甚至 NSDecimalNumber(甚至建议存储代表金额的值)。