美文网首页
OC中的数据精度处理和数据类型解析(一)

OC中的数据精度处理和数据类型解析(一)

作者: LiuffSunny | 来源:发表于2020-03-19 20:40 被阅读0次

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

    相关文章

      网友评论

          本文标题:OC中的数据精度处理和数据类型解析(一)

          本文链接:https://www.haomeiwen.com/subject/segfyhtx.html