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);



转载于:https://www.cnblogs.com/soulDn/p/10827053.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值