在做和金額支付相關的時候,對價格的計算要求是非常嚴格的。
放一個金額四則運算的方法
// 金額計算方式
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,可以直接運行測試