iOS NSDecimalNumber详解

  1. NSDecimalNumber
    NSDecimalNumber是iOS系统的api,是一种精确计算。主要解决一下问题:
    1、字符串转float等不精确问题。
    2、精确计算
    3、保留小数位数
    4、四舍五入及其它的保留小数位数规则
  2. NSDecimalNumber的运算
 //1、字符串 -> NSDecimalNumber
    NSDecimalNumber *num1 = [NSDecimalNumber decimalNumberWithString:@"123"];
    NSLog(@"num1 == %@",num1);
    //2、NSNumber -> NSDecimalNumber(基础类型的话,先转成NSNumber)
    NSDecimalNumber *num2 = [NSDecimalNumber decimalNumberWithDecimal:[[NSNumber numberWithInt:123] decimalValue]];
    NSLog(@"num2 == %@",num2);

    //相关运算


    /*加 +
    - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber;
    - (NSDecimalNumber *)decimalNumberByAdding:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id )behavior;//带保留小数位数
     */

    NSDecimalNumber *num3 = [num1 decimalNumberByAdding:num2];
    NSLog(@"num3 == %@",num3);


    /*减 -
     - (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber;
     - (NSDecimalNumber *)decimalNumberBySubtracting:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id )behavior;//带保留小数位数
     */
    NSDecimalNumber *num4 = [num1 decimalNumberBySubtracting:num2];
    NSLog(@"num4 == %@",num4);

    /*乘 *
     - (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber;
     - (NSDecimalNumber *)decimalNumberByMultiplyingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id )behavior;//带保留小数位数
     */
    NSDecimalNumber *num5 = [num1 decimalNumberByMultiplyingBy:num2];
    NSLog(@"num5 == %@",num5);

    /*除 \
     - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber;
     - (NSDecimalNumber *)decimalNumberByDividingBy:(NSDecimalNumber *)decimalNumber withBehavior:(nullable id )behavior;//带保留小数位数
     */
    NSDecimalNumber *num6 = [num1 decimalNumberByDividingBy:num2];
    NSLog(@"num6 == %@",num6);

    /*比较 
     - (NSComparisonResult)compare:(NSNumber *)decimalNumber;
     */

    NSComparisonResult result = [num1 compare:num2];
    NSLog(@"result == %ld",(long)result);


    //保留小数及相关参数含义
    /*保留小数
     + (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
     */
   //1、 NSRoundingMode

    // Rounding policies :
    // Original
    //    value 1.2  1.21  1.25  1.35  1.27
    // Plain    1.2  1.2   1.3   1.4   1.3
    // Down     1.2  1.2   1.2   1.3   1.2
    // Up       1.2  1.3   1.3   1.4   1.3
    // Bankers  1.2  1.2   1.2   1.4   1.3
   /* typedef NS_ENUM(NSUInteger, NSRoundingMode) {
        NSRoundPlain,   // Round up on a tie        四舍五入
        NSRoundDown,    // Always down == truncate  向下保留
        NSRoundUp,      // Always up                向上保留
        NSRoundBankers  // on a tie round so last digit is even 貌似是:
       if(四舍五入位==5){
           if(5后有非0数字){//如:1.251  - 1.26
                 入
            }
             else{//如:1.25 - 1.25
                 舍
            }
        }else{
            四舍五入
        }

    };
    */
    //2、scale 保留小数个数
    //3、其它:溢出等是否报错

    NSDecimalNumber *num_1 = [NSDecimalNumber decimalNumberWithString:@"1.2"];
    NSDecimalNumber *num_2 = [NSDecimalNumber decimalNumberWithString:@"3.4"];
    NSDecimalNumberHandler *handel = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundBankers scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];

    NSDecimalNumber *num_3 = [num_1 decimalNumberByDividingBy:num_2 withBehavior:handel];
    NSLog(@"num_3 == %@",num_3);


//打印结果
    /*
     2015-12-11 13:26:28.269 test[32280:9906455] num1 == 123
     2015-12-11 13:26:28.269 test[32280:9906455] num2 == 123
     2015-12-11 13:26:28.269 test[32280:9906455] num3 == 246
     2015-12-11 13:26:28.270 test[32280:9906455] num4 == 0
     2015-12-11 13:26:28.270 test[32280:9906455] num5 == 15129
     2015-12-11 13:26:28.270 test[32280:9906455] num6 == 1
     2015-12-11 13:26:28.270 test[32280:9906455] result == 0
     2015-12-11 13:26:28.270 test[32280:9906455] num_3 == 0.35
     */
  1. 个人封装

https://github.com/GitHubXuLiying/NSDecimalNumber-Addtion.git

你可能感兴趣的:(iOS)