码迷,mamicode.com
首页 > Web开发 > 详细

json解析精度问题

时间:2016-06-21 17:46:34      阅读:474      评论:0      收藏:0      [点我收藏+]

标签:

由于后台传过来的值是float或者double类型,解析之后作为对象放到字典或数组里面,被转成nsstring或者nsnumber会有一定的误差,很正常

解决办法,通过NSNumberFormatter,可以设置NSNumber输出的格式;

NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];

 formatter.numberStyle = NSNumberFormatterDecimalStyle;

 NSString *string = [formatter stringFromNumber:[NSNumber numberWithInt:123.85999999999]];

 NSLog(@"Formatted number string:%@",string);

//枚举类型

typedef NS_ENUM(NSUInteger, NSNumberFormatterRoundingMode) {

    NSNumberFormatterRoundCeiling = kCFNumberFormatterRoundCeiling,

    NSNumberFormatterRoundFloor = kCFNumberFormatterRoundFloor,

    NSNumberFormatterRoundDown = kCFNumberFormatterRoundDown,

    NSNumberFormatterRoundUp = kCFNumberFormatterRoundUp,

    NSNumberFormatterRoundHalfEven = kCFNumberFormatterRoundHalfEven,

    NSNumberFormatterRoundHalfDown = kCFNumberFormatterRoundHalfDown,

    NSNumberFormatterRoundHalfUp = kCFNumberFormatterRoundHalfUp

};

 

NSNumberFormatterRoundCeiling = kCFNumberFormatterRoundCeiling,//四舍五入,直接输出124

    NSNumberFormatterRoundFloor = kCFNumberFormatterRoundFloor,//保留小数输出123.86

    NSNumberFormatterRoundDown = kCFNumberFormatterRoundDown,//加上了人民币标志

    NSNumberFormatterRoundUp = kCFNumberFormatterRoundUp,//百分号表示

    NSNumberFormatterRoundHalfEven = kCFNumberFormatterRoundHalfEven

    NSNumberFormatterRoundHalfDown = kCFNumberFormatterRoundHalfDown

    NSNumberFormatterRoundHalfUp = kCFNumberFormatterRoundHalfUp 

json解析精度问题

标签:

原文地址:http://www.cnblogs.com/llhlj/p/5604141.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!