金额的一些常规处理方法

2017-07-11  本文已影响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];
}
上一篇下一篇

猜你喜欢

热点阅读