美文网首页iOS 应用层
iOS中Double转换NSString 精度不准确

iOS中Double转换NSString 精度不准确

作者: 爱你zzp | 来源:发表于2018-02-27 16:11 被阅读0次

    今天遇到了一个问题,本来json中返回的是Double类型,值为9.87,显示到Label 却是9.86999999999999

    为了方便大家理解,我先做了如下演示

    NSNumber *amount = [NSNumber numberWithDouble:9.87];
    NSLog(@"normal amount %@", amount);
    
    NSNumber *amount1 = [NSNumber numberWithDouble:3.87];   
    NSLog(@"normal amount1 %@", amount1);
    
    打印的值为
    Test[8924:1150018] normal amount 9.869999999999999
    Test[8924:1150018] normal amount1 3.87
    

    有问题,就要开始解决,经过一番查找,找到了以下解决办法

    //1
    //该方法中会规定小数点的位数
    NSString *amountString1 = [NSString stringWithFormat:@"%.2f",[amount doubleValue]];
    NSLog(@"amountString1 %@",amountString1);
    

    //2    
    double amountDouble = [amount doubleValue];
    
    NSString *amountDoubleStr = [NSString stringWithFormat:@"%lf", amountDouble];
    NSDecimalNumber *amountNum = [NSDecimalNumber decimalNumberWithString:amountDoubleStr];
    
    NSString *amountStr = [amountNum stringValue];
    NSLog(@"amountStr %@", amountStr);
    
    打印的值为
    Test[9451:1317790] amountStr 9.87
    

    //3
    //该方法中会规定小数点的位数
    NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
    formatter.maximumFractionDigits = 2;
    
    NSString *amountString = [formatter stringFromNumber:amount];
    NSLog(@"amountString %@",amountString);
    
    打印的值为
    Test[9451:1317790] amountString 9.87
    

    //4
    NSNumberFormatter *formatter2 = [[NSNumberFormatter alloc] init];
    formatter2.numberStyle = NSNumberFormatterDecimalStyle;
    //去掉千位分隔符,举例123,456.88加了这句会变成123456.88
    [formatter2 setGroupingSeparator:@""]; 
     //formatter2.maximumFractionDigits 默认值是3
    
    NSString *amountString2 = [formatter2 stringFromNumber:amount];
    NSLog(@"amountString2 %@",amountString2);
    
    打印的值为
    Test[9451:1317790] amountString2 9.87
    

    //建议添加category

    //.h
    @interface NSNumber (Cd)
    
    - (NSString *)LGDoubleStringValue;
    
    - (NSString *)LGDoubleStringValueWithDigits:(NSUInteger)digits;
    
    @end
    

    //.m
    @implementation NSNumber (Cd)
    
    - (NSString *)LGDoubleStringValue
    {
        return [self LGDoubleStringValueWithDigits:2];
    }
    
    - (NSString *)LGDoubleStringValueWithDigits:(NSUInteger)digits
    {
        NSNumberFormatter *formatter = [[NSNumberFormatter alloc] init];
        formatter.numberStyle = NSNumberFormatterDecimalStyle;
        formatter.maximumFractionDigits = digits;
        [formatter setGroupingSeparator:@""];
        
        NSString *retString = [formatter stringFromNumber:self];
        return retString;
    }
    
    @end
    

    //使用方法

    NSLog(@"%@",@(9.8712132123));
    NSLog(@"%@",[@(9.87123131) LGDoubleStringValue]);
    NSLog(@"%@",[@(9.871231313) LGDoubleStringValueWithDigits:5]);
    
    //输出结果为
    Test[10318:1500287] 9.871213212300001
    Test[10318:1500287] 9.87
    Test[10318:1500287] 9.87123
    
    参考以下网址

    出现问题的原因 https://www.aliyun.com/jiaocheng/400490.html
    https://stackoverflow.com/questions/24246565/objective-c-double-to-nsstring-with-x-many-decimals
    NSNumberFormatter属性

    相关文章

      网友评论

        本文标题:iOS中Double转换NSString 精度不准确

        本文链接:https://www.haomeiwen.com/subject/jedmxftx.html