一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

iOS开发之货币计算的例子

时间:2022-06-25 23:45:04 编辑:袖梨 来源:一聚教程网


在做和金额支付相关的时候,对价格的计算要求是非常严格的。
放一个金额四则运算的方法


//  金额计算方式
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];
}

上面的只是方法,下面奉上demo,可以直接运行测试

热门栏目