浮点数相关精度输出问题
2019-03-05 本文已影响0人
大写的空气
正常输出浮点数
double d = 1234.567890;
NSString *strD1 = [NSString stringWithFormat:@"%f", d]; //结果为@"1234.567890"
NSString *strD2 = [NSString stringWithFormat:@"%.2f", d]; //保留小数点后两位 结果为1234.57
NSString *strD3 = [NSString stringWithFormat:@"%0.2f", d]; //保留小数点后两位 结果为1234.57
short p =8;
NSString *strP = [NSString stringWithFormat:@"%%.%df",p];
NSString *strD4 = [NSString stringWithFormat:strP, d]; //动态保留小数点位数 结果为1234.56789000
NSString *strD5 = [NSNumber numberWithDouble:d].getNoStyleDecimal ; //NSDecimalNumber *dn = [NSDecimalNumber decimalNumberWithString:self.stringValue]; return dn.stringValue; 结果为1234.56789
当d=0.000000001时,结果如下