iOS NSDecimalNumber 使用
在iOS开发中,经常遇到和货币价格计算相关的,这时就需要注意计算精度的问题。使用float类型运算,经常出现误差。为了解决这种问题我们使用NSDecimalNumber,下面将通过例子的形式给大家展示一下。
1.加法的运算
//方式1:不进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",3.155]];
self.number2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",6.1]];
NSDecimalNumber *num = [self.number2 decimalNumberByAdding:self.number1];
NSLog(@"num===%@",num);
//方式2:进行四舍五入
NSRoundUp属性使所有的操作算到最近的位置,其他的进位选项是NSRoundPlain, NSRoundDown, 和 NSRoundBankers,它们都被定义在NSRoundingMode,scale参数定义了结果值保留的小数位的数量,其余的参数给所有的操作定义了异常处理行为.
//scale四舍五入,舍入位数
NSDecimalNumberHandler *roundUp = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number2 decimalNumberByAdding:self.number1 withBehavior:roundUp];
NSLog(@"num===%@",num);
2.减法运算
//方式1:不进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",10.1543]];
self.number2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",2.3]];
NSDecimalNumber *num = [self.number1 decimalNumberBySubtracting:self.number2];
NSLog(@"num===%@",num);
//方式2:进行四舍五入
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number1 decimalNumberBySubtracting:self.number2 withBehavior:handler];
NSLog(@"num===%@",num);
3.乘法的运算
//方式1:不进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",10.001]];
self.number2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
NSDecimalNumber *num = [self.number1 decimalNumberByMultiplyingBy:self.number2];
NSLog(@"num===%@",num);
//方式2:进行四舍五入
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number1 decimalNumberByMultiplyingBy:self.number2 withBehavior:handler];
NSLog(@"num===%@",num);
4.除法的运算
//方式1:不进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%f",10.14]];
self.number2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
NSDecimalNumber *num = [self.number1 decimalNumberByDividingBy:self.number2];
NSLog(@"num===%@",num);
//方式2:进行四舍五入
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:1 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number1 decimalNumberByDividingBy:self.number2 withBehavior:handler];
NSLog(@"num===%@",num);
5.比较
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
self.number2 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
//self.number1 < self.number2
if ([self.number1 compare:self.number2] == NSOrderedAscending) {
NSLog(@"self.number1 < self.number2");
}else if([self.number1 compare:self.number2] ==NSOrderedDescending){//self.number1 > self.number2
NSLog(@"self.number1 > self.number2");
}else if ([self.number1 compare:self.number2] == NSOrderedSame){
//self.number1 = self.number2
NSLog(@"self.number1 = self.number2");
}
6.指数运算
//方式1:不进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
NSDecimalNumber *num = [self.number1 decimalNumberByMultiplyingByPowerOf10:2];
//方式2:进行四舍五入
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number1 decimalNumberByMultiplyingByPowerOf10:2 withBehavior:handler];
NSLog(@"~~~%@",num);
7.次方运算
//方式1:进行四舍五入
self.number1 = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d",2]];
NSDecimalNumber *num = [self.number1 decimalNumberByRaisingToPower:2];
//方式2:进行四舍五入
NSDecimalNumberHandler *handler = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:YES];
NSDecimalNumber *num = [self.number1 decimalNumberByRaisingToPower:4 withBehavior:handler];
NSLog(@"~~~%@",num);