我们在开发中容易遇到浮点型精度丢失
-
比如2.36会变成2.359999999
-
如何把浮点型正确显示到界面呢?
NSDecimalNumber * number = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%lf",2.36]]; NSDecimal resultStruct; NSDecimal str = number.decimalValue; NSDecimalRound(&resultStruct, &str, 3, NSRoundPlain); // 四舍五入保留3为小数,末尾没有0 NSDecimalNumber * resultNumber = [NSDecimalNumber decimalNumberWithDecimal:resultStruct]; NSString * str = [resultNumber stringValue];
网友评论