iOS - Json解析精度丢失处理(NSString, Double, Float)
开发中处理处理价格金额问题, 后台经常返回float类型, 打印或转成NSString都会有精度丢失问题, 因此使用系统自带的NSDecimalNumber做处理, 能解决这问题:
经过测试其实系统NSDecimalNumber是对有问题值做了四舍五入处理
• 还有经过测试, 重要的事说三遍:
处理精度有关的数据请用double
处理精度有关的数据请用double
处理精度有关的数据请用double
float testDouble = [jsonDict[@"Body"][@"Amount"] floatValue]; //有问题 90.989999999999994
NSString *convertString = decimalNumberWithString([jsonDict[@"Body"][@"Amount"] stringValue]);
NSLog(@"%@", convertString);
testDouble的值 原始值& NSDecimalNumber处理后打印后的值
// 99.489999999999994 99.49
// 99.989999999999994 99.99
// 90 90.00
// 90.090000000000003 90.09
// 90.189999999999998 90.19
// 90.290000000000006 90.29
// 90.39 90.39
// 90.489999999999994 90.49
// 90.590000000000003 90.59
// 90.689999999999998 90.69
// 90.790000000000006 90.79
// 90.89 90.89
// 90.989999999999994 90.99
对此自己写了个方法处理 :
/** 直接传入精度丢失有问题的Double类型*/
NSString *decimalNumberWithDouble(double conversionValue){
NSString *doubleString = [NSString stringWithFormat:@"%lf", conversionValue];
NSDecimalNumber *decNumber = [NSDecimalNumber decimalNumberWithString:doubleString];
return [decNumber stringValue];
}
强烈建议 :
有关浮点型数据,后台传字符串的格式,防止丢失精度。
iOS - Json解析精度丢失处理(NSString, Double, Float)的更多相关文章
- iOS json 解析遇到error: Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed.
Error Domain=NSCocoaErrorDomain Code=3840 "The operation couldn’t be completed. (Cocoa error 38 ...
- iOS json解析的几种方法 NSJSONSerialization,JSONKit,SBJson ,TouchJson
相关的第三方类库大家可以去github上下载 1.NSJSONSerialization 具体代码如下 : - (void)viewDidLoad { [super viewDidLoad]; NSD ...
- iOS JSON解析
解析json成dic对象 -(void)fetchedData:(NSData*)responseData {//parse out the json dataNSError* error; NSDi ...
- json解析精度问题
由于后台传过来的值是float或者double类型,解析之后作为对象放到字典或数组里面,被转成nsstring或者nsnumber会有一定的误差,很正常 解决办法,通过NSNumberFormatte ...
- iOS json解析中包含“\n”等解析出错
文题算是解决了,把特殊字符替换一下:-(NSString *)JSONString:(NSString *)aString { NSMutableString *s = [NSMutableSt ...
- MySQL5.7在JSON解析后丢失小数部分的Bug
在MySQL Bugs上提交了 https://bugs.mysql.com/bug.php?id=84935 . 已经在MySQL8.0.1中修复 重现步骤 -- Prepare the table ...
- iOS - JSON 数据解析
iOS - JSON 数据解析 前言 NS_CLASS_AVAILABLE(10_7, 5_0) @interface NSJSONSerialization : NSObject @availab ...
- iOS项目double、float精度丢失解决办法
描述 在iOS项目中老是遇到double.float精度丢失的问题 PS: NSString * jsonStr = @"{\"9.70\":9.70,\"67 ...
- json 解析double类型多出几位小数,利用NSDecimalNumber修复 , 服务器返回的数据,用数据类型接收(int,NSNumber)?
后台返回float类型数据8.2,实际用AFN的json解析后得到8.1999999999,该怎么处理呢? 开发中处理处理价格金额问题, 后台经常返回float.double类型, 打印或转成NSS ...
随机推荐
- Android 裁剪图片为圆形图片
转自http://blog.csdn.net/kkmike999/article/details/16359713 /** * 转换图片成圆形 * * @param bitmap * 传入Bitmap ...
- scikit-learn 逻辑回归类库使用小结
之前在逻辑回归原理小结这篇文章中,对逻辑回归的原理做了小结.这里接着对scikit-learn中逻辑回归类库的我的使用经验做一个总结.重点讲述调参中要注意的事项. 1. 概述 在scikit-lear ...
- angularjs 请求后端接口请求了两次
用angularjs的过程中发现,每次打开页面,请求后端的接口都请求了两次 如下图可以看到, http://192.168.1.109:8080/zdh/api/v1/goods/54 这个页面loa ...
- JSP页面静态化
Ps:好久没写博客了,不是我太懒,是因为苦逼的我出差去上海了,天天加班 刚回成都.... 今天说说jsp页面静态化,知道静态化的朋友都不陌生,说白了就是访问后缀是html 而不是jsp. 没听说过静态 ...
- MyCAT实现MySQL的读写分离
在MySQL中间件出现之前,对于MySQL主从集群,如果要实现其读写分离,一般是在程序端实现,这样就带来一个问题,即数据库和程序的耦合度太高,如果我数据库的地址发生改变了,那么我程序端也要进行相应的修 ...
- 1Z0-053 争议题目解析304
1Z0-053 争议题目解析304 考试科目:1Z0-053 题库版本:V13.02 题库中原题为: 304.What privileges must be granted to allow an a ...
- TP框架执行流程分许
1. index.php 入口文件 2. ThinkPHP/ThinkPHP.php 在php5.3版本以后 设置常量有两种方式: const name = value; 作用域根据当前命名空间决定 ...
- git取消跟踪文件
取消跟踪文件: $git rm --cached FILENAME 取消跟踪目录: $git rm --cached FILENAME -r
- Mybatis XML 映射配置文件 -- 熟悉配置
来源:http://www.mybatis.org/mybatis-3/zh/configuration.html properties mybatis读取属性顺序. 如果属性在不只一个地方进行了配置 ...
- Cache-Aside Pattern(缓存模式)
Load data on demand into a cache from a data store. This pattern can improve performance and also he ...