美文网首页iOS技术专题
NSDecimalNumber 介绍

NSDecimalNumber 介绍

作者: 小孩仔 | 来源:发表于2017-11-07 15:52 被阅读0次

    NSDecimalNumber是NSNumber的不可变子类。苹果针对浮点型计算时存在精度计算误差的问题而提供的一个计算类,它是基于10进制的定点计算保证了精度不会缺失。同时也可以定制精度的取正类型:向上取正、向下去正、四舍五入等。相对与浮点类型的计算,NSDecimalNumber提供了更加精准的计算。

    一个定点数包含了:用一个尾数(Mantissa)、一个基数(Base)、一个指数(Exponent)以及一个表示正负的符号(sign).
    比如 15.99 用十进制科学计数法可以表达为 +1599 × 10⁻² ,其中 1.2345 为尾数,10 为基数,2 为指数。sign为 ‘+’。

    代码表示:

        NSDecimalNumber*price;
        price = [NSDecimalNumberdecimalNumberWithMantissa:1599
                                                exponent:-2   //10的-2次方
                                                isNegative:NO];
     等效
         price = [NSDecimalNumberdecimalNumberWithString:@"15.99"];
    
    • (NSDecimalNumber *)decimalNumberWithMantissa:(unsigned long long)mantissa exponent:(short)exponent isNegative:(BOOL)flag;
      mantissa:长整形;exponent:指数;flag:正负数。

        NSDecimalNumber *subtotalAmount = [NSDecimalNumber   decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO];   //12.75
        subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:2 isNegative:YES];   //-127500
      
    • (NSDecimalNumber *)decimalNumberWithDecimal:(NSDecimal)dcm;
      将C语言NSDecimal类型转成一个十进制数。

         NSDecimalNumber *subtotalAmount = [NSDecimalNumber decimalNumberWithMantissa:1275 exponent:-2 isNegative:NO];   //12.75
         NSDecimal decimalValue = [subtotalAmount decimalValue];
        /**
         *  数据结构:
        (NSDecimal) decimalValue = {
        _exponent = -2
        _length = 1
        _isNegative = 0
        _isCompact = 1
        _reserved = 0
        _mantissa = ([0] = 1275, [1] = 0, [2] = 0, [3] = 0, [4] = 0, [5] = 0, [6] = 0, [7] = 0)
      }
        */
        subtotalAmount = [NSDecimalNumber decimalNumberWithDecimal:decimalValue];   //12.75
      
    • (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue;
      将字符串转成一个十进制数。

      NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"-12.74"];      //-12.74
      discountAmount = [NSDecimalNumber decimalNumberWithString:@"127.4"];      //127.4
      
    • (NSDecimalNumber *)decimalNumberWithString:(nullable NSString *)numberValue locale:(nullable id)locale;
      这个有点复杂,locale代表一种格式,就像date的格式化一样。这里的locale可以传递两种格式
      NSDictionary类型:

      NSDictionary *locale = [NSDictionary dictionaryWithObject:@","     forKey:NSLocaleDecimalSeparator];    //以","当做小数点格式
      NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"123,40" locale:locale];    //123.4
      
    •   NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"fr_FR"];  
      //法国数据格式,法国的小数点是','逗号
      NSDecimalNumber *discountAmount = [NSDecimalNumber decimalNumberWithString:@"123,40" locale:locale];    //123.4
      
    •  +(NSDecimalNumber *)zero; //0
       +(NSDecimalNumber *)one; //1
       +(NSDecimalNumber *)minimumDecimalNumber;
       +(NSDecimalNumber *)maximumDecimalNumber;
       +(NSDecimalNumber *)notANumber;
       //非数字,常用于对比,比如:
      [[NSDecimalNumber notANumber] isEqualToNumber:myNumber];
      

    加法运算

      -(NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
      -(NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    减法运算

      -(NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
      -(NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    乘法运算

     -(NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    除法运算

     -(NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
     -(NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    a的n次方

     -(NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power;
     -(NSDecimalNumber *)decimalNumberByRaisingToPower:(NSUInteger)power withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    指数运算

     -(NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power;
     -(NSDecimalNumber *)decimalNumberByMultiplyingByPowerOf10:(short)power 
    withBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    四舍五入运算

      -(NSDecimalNumber *)decimalNumberByRoundingAccordingToBehavior:(nullable id <NSDecimalNumberBehaviors>)behavior;
    

    比较运算

     -(NSComparisonResult)compare:(NSNumber *)decimalNumber;
    

    示例

     NSDecimalNumber *discount1 = [NSDecimalNumber decimalNumberWithString:@"1.2"];
    NSDecimalNumber *discount2 = [NSDecimalNumber decimalNumberWithString:@"1.3"];
    NSComparisonResult result = [discount1 compare:discount2];
    if (result == NSOrderedAscending) {
        NSLog(@"1.2 < 1.3");
    } else if (result == NSOrderedSame) {
        NSLog(@"1.2 == 1.3");
    } else if (result == NSOrderedDescending) {
        NSLog(@"1.2 > 1.3");
    }
    

    加减乘除平方示例

     NSDecimalNumber*price1 = [NSDecimalNumberdecimalNumberWithString:@"15.99"];
     NSDecimalNumber*price2 = [NSDecimalNumberdecimalNumberWithString:@"29.99"];
     NSDecimalNumber*coupon = [NSDecimalNumberdecimalNumberWithString:@"5.00"];
     NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
     NSDecimalNumber*numProducts = [NSDecimalNumberdecimalNumberWithString:@"2.0"];
    
     **加**
     NSDecimalNumber *subtotal = [price1 decimalNumberByAdding:price2];  
     **减**
     NSDecimalNumber *afterCoupon = [subtotal decimalNumberBySubtracting:coupon]; 
    **除**
     NSDecimalNumber *afterDiscount = [afterCoupon  decimalNumberByMultiplyingBy:discount];
    **平均**
     NSDecimalNumber *average = [afterDiscount decimalNumberByDividingBy:numProducts];
    

    平均价的平方
    NSDecimalNumber*averageSquared = [average decimalNumberByRaisingToPower:2];

    的NSLog(@“小计:%@”,加后);/ / 45.98
    的NSLog(@“优惠券后:%@”,减);/ / 40.98
    的NSLog((@“折后:%@”),9/10);/ / 36.882
    的NSLog(@“每股平均价产品:%@”,平均);/ / 18.441
    的NSLog(@“平均价的平方:%@”,averageSquared);/ / 340.070481
    

    保留小数点后两位

    NSDecimalNumberHandler*roundUp = [NSDecimalNumberHandler
                                      decimalNumberHandlerWithRoundingMode:NSRoundUp
                                      scale:2
                                      raiseOnExactness:NO
                                      raiseOnOverflow:NO
                                      raiseOnUnderflow:NO
                                      raiseOnDivideByZero:YES];
    
     //9折后 36.882 有余进位结果 36.89
    
     NSDecimalNumber*subtotal = [NSDecimalNumberdecimalNumberWithString:@"40.98"];
     NSDecimalNumber*discount = [NSDecimalNumberdecimalNumberWithString:@".90"];
     NSDecimalNumber*total = [subtotal decimalNumberByMultiplyingBy:discount
                                                   withBehavior:roundUp];
     NSLog(@"Rounded total: %@", total);
    

    希望可以帮助大家 如果哪里有什么不对或者不足的地方,还望读者多多提意见 !

    相关文章

      网友评论

        本文标题:NSDecimalNumber 介绍

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