美文网首页
金额的一些常规处理方法

金额的一些常规处理方法

作者: 江湖闹士 | 来源:发表于2017-07-11 10:36 被阅读14次

    1、将金额按千位逗号分隔

    //金钱加,分隔符
    - (NSString *)countNumAndChangeformat{
        
        NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init];
        [numberFormatter setPositiveFormat:@"###,##0.######;"];
        NSString *formattedNumBerString = [numberFormatter stringFromNumber:[NSNumber numberWithDouble:[self doubleValue]]];
        return formattedNumBerString;
    }
    

    扩展

    对数值677789.98做一下格式处理:
    [numberFormatter setPositiveFormat:@",###.00”];//输出:677,789.98 
    [numberFormatter setPositiveFormat:@".00;”]//输出:677789.98  
    [numberFormatter setPositiveFormat:@“0%;”]//输出:67778998%  
    [numberFormatter setPositiveFormat:@“0.00%;”]//输出:67778998.00%
    

    2、金额之间的四则运算

    //  金额计算方式
    typedef enum {
        Adding,
        Subtracting,
        Multiplying,
        Dividing,
    }calucateWay;
    
    #pragma mark 货币计算
    + (NSString *)decimalNumberCalucate:(NSString *)originValue1 originValue2:(NSString *)originValue2 calucateWay:(calucateWay)calucateWay
    {
        NSDecimalNumber *decimalNumber1 = [NSDecimalNumber decimalNumberWithString:originValue1];
        NSDecimalNumber *decimalNumber2 = [NSDecimalNumber decimalNumberWithString:originValue2];
        NSDecimalNumber *product;
        switch (calucateWay) {
            case Adding:
                NSLog(@"+++++++++");
                product = [decimalNumber1 decimalNumberByAdding:decimalNumber2];
                break;
                 
            case Subtracting:
                NSLog(@"---------");
                product = [decimalNumber1 decimalNumberBySubtracting:decimalNumber2];
                break;
                 
            case Multiplying:
                NSLog(@"*********");
                product = [decimalNumber1 decimalNumberByMultiplyingBy:decimalNumber2];
                break;
                 
            case Dividing:
                NSLog(@"/////////");
                product = [decimalNumber1 decimalNumberByDividingBy:decimalNumber2];
                break;
                 
            default:
                break;
        }
        return [product stringValue];
    }
    

    相关文章

      网友评论

          本文标题:金额的一些常规处理方法

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