由于后台传过来的值是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