美文网首页
NSDecimalNumber的加减乘除

NSDecimalNumber的加减乘除

作者: MELON_DARLING | 来源:发表于2020-04-16 16:56 被阅读0次
        //number a
        NSDecimalNumber * a = [NSDecimalNumber decimalNumberWithString:@"2"];
        //number b
        NSDecimalNumber * b = [NSDecimalNumber decimalNumberWithString:@"2"];
        
        //加法
        NSDecimalNumber * add = [a decimalNumberByAdding:b];
        //减法
        NSDecimalNumber * subtract = [a decimalNumberBySubtracting:b];
        //乘法
        NSDecimalNumber * multiply = [a decimalNumberByMultiplyingBy:b];
        //除法
        NSDecimalNumber * divid = [a decimalNumberByDividingBy:b];
    
    输出以上算法的结果: 屏幕快照 2020-04-16 16.06.44.png

    NSDecimalNumberHandler:

    /**
     初始化方法
     @param roundingMode 舍入方式
     @param scale 小数点后舍入值的位数
     @param exact 精度错误处理
     @param overflow 溢出错误处理
     @param underflow 下溢错误处理
     @param divideByZero 除以0的错误处理
     @return NSDecimalNumberHandler对象
     */
    + (instancetype)decimalNumberHandlerWithRoundingMode:(NSRoundingMode)roundingMode scale:(short)scale raiseOnExactness:(BOOL)exact raiseOnOverflow:(BOOL)overflow raiseOnUnderflow:(BOOL)underflow raiseOnDivideByZero:(BOOL)divideByZero;
    

    NSRoundingMode 的类型

    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
    };
    
    NSRoundPlain 是四舍五入
    NSRoundDown 是向下取整
    NSRoundUp 是向上取整
    NSRoundBankers 是在四舍五入的基础上,加上末尾数为5时,变成偶数的规则
    

    下面是我项目中的例子,具体的数值是:提现5.85元、费率为9%、单笔手续费一块钱

        //提现金额
        //手续费 = 提现金额*费率+单笔手续费
        //实际到账 = 提现金额 - 手续费
        //以下算法 货币类型 保证小数精度不丢失
        
        //要提现的金额 元
        NSDecimalNumber * amountValueNumber = [NSDecimalNumber decimalNumberWithString:money];
        NSString * singleFeeStr = [NSString stringWithFormat:@"%@",accountInfo.singleFee];
        NSDecimalNumber * singFeeValueNumber = [NSDecimalNumber decimalNumberWithString:singleFeeStr];
        //费率转换:除以100
        NSDecimalNumber * singFeeHundredNumber = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d", 100]];
        NSDecimalNumber * singleFeeNumber = [singFeeValueNumber decimalNumberByDividingBy:singFeeHundredNumber];
        
         //费率 eg:3% 以下费率相关都用3距离
        NSString * rateStr = [NSString stringWithFormat:@"%@%%",accountInfo.rate];
        //费率:3
        NSDecimalNumber * rateValueNumber = [NSDecimalNumber decimalNumberWithString:rateStr];
        //费率转换:除以100
        NSDecimalNumber *oneHundredValueNumber = [NSDecimalNumber decimalNumberWithString:[NSString stringWithFormat:@"%d", 100]];
        //费率转小数:0.03
        NSDecimalNumber *rate = [rateValueNumber decimalNumberByDividingBy:oneHundredValueNumber];
        //根据费率产生的手续费(四舍五入之前数值)
        NSDecimalNumber *fee = [amountValueNumber decimalNumberByMultiplyingBy:rate];
        //四舍五入之后的手续费
        NSDecimalNumberHandler *roundingBehavior = [NSDecimalNumberHandler decimalNumberHandlerWithRoundingMode:NSRoundPlain scale:2 raiseOnExactness:NO raiseOnOverflow:NO raiseOnUnderflow:NO raiseOnDivideByZero:NO];
        NSString *feeStr =[[fee decimalNumberByRoundingAccordingToBehavior:roundingBehavior] stringValue];
        //需求上手续费需要四舍五入
        NSDecimalNumber * feeDecimal = [NSDecimalNumber decimalNumberWithString:feeStr];
        
        //总手续费 费率手续费+单笔
        NSDecimalNumber * totalFeeNumber = [feeDecimal decimalNumberByAdding:singleFeeNumber];
        NSDecimalNumber * actualDecimal = [amountValueNumber decimalNumberBySubtracting:totalFeeNumber];
        NSLog(@"\n四舍五入之前的手续费%@\n提现金额%@\n提现费率%@\n手续费%@\n单笔加%@\n实际到账%@",fee,amountValueNumber,rateStr,feeDecimal,singleFeeNumber,actualDecimal);
    

    输出的具体数值:


    屏幕快照 2020-04-16 16.49.24.png

    相关文章

      网友评论

          本文标题:NSDecimalNumber的加减乘除

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