zoukankan      html  css  js  c++  java
  • 关于计算的工具类总结

      1 package cn.common.utils;
      2 
      3 import java.math.BigDecimal;
      4 
      5 import org.apache.commons.lang3.math.NumberUtils;
      6 
      7 import com.google.common.base.Optional;
      8 
      9 /**
     10 * @ClassName: DecimalCalculate
     11 * @Description: 计算公式
     12 * @author happiness
     13 * @date 2018年8月25日 下午10:37:36
     14 * @version V1.0 
     15 */
     16 public class DecimalCalculate {
     17     //默认保留小数点位数
     18     private static int SCALE = 4;
     19     
     20     /**
     21      * String的加法运算封装
     22      * @param before
     23      * @param after
     24      * @return
     25      */
     26     public static BigDecimal safeAdd ( String before, String after ) {
     27         if ((!StringUtil.isNumeric(before)) || (!StringUtil.isNumeric(after)))
     28             return BigDecimal.ZERO;
     29         return NumberUtils.createBigDecimal(before)
     30                 .add( NumberUtils.createBigDecimal(after));
     31     }
     32     
     33     /**
     34      * BigDecimal的加法运算封装
     35      * @param b1
     36      * @param bn
     37      * @return
     38      */
     39    public static BigDecimal safeAdd(BigDecimal b1, BigDecimal... bn) {
     40        if (null == b1) {
     41            b1 = BigDecimal.ZERO;
     42        }
     43        if (null != bn) {
     44            for (BigDecimal b : bn) {
     45                b1 = b1.add(null == b ? BigDecimal.ZERO : b);
     46            }
     47        }
     48        return b1;
     49    }
     50  
     51    /**
     52     * Integer加法运算的封装
     53     * @param b1   第一个数
     54     * @param bn   需要加的加法数组
     55     * @注 : Optional  是属于com.google.common.base.Optional<T> 下面的class
     56     * @return
     57     */
     58    public static Integer safeAdd(Integer b1, Integer... bn) {
     59        if (null == b1) {
     60            b1 = 0;
     61        }
     62        Integer r = b1;
     63        if (null != bn) {
     64            for (Integer b : bn) {
     65                r += Optional.fromNullable(b).or(0);
     66            }
     67        }
     68        return r > 0 ? r : 0;
     69    }
     70  
     71    /**
     72     * 计算金额方法
     73     * @param b1
     74     * @param bn
     75     * @return
     76     */
     77    public static BigDecimal safeSubtract(BigDecimal b1, BigDecimal... bn) {
     78        return safeSubtract(true, b1, bn);
     79    }
     80  
     81    /**
     82     * BigDecimal的安全减法运算
     83     * @param isZero  减法结果为负数时是否返回0,true是返回0(金额计算时使用),false是返回负数结果
     84     * @param b1 被减数
     85     * @param bn 需要减的减数数组
     86     * @return
     87     */
     88    public static BigDecimal safeSubtract(Boolean isZero, BigDecimal b1, BigDecimal... bn) {
     89        if (null == b1) {
     90            b1 = BigDecimal.ZERO;
     91        }
     92        BigDecimal r = b1;
     93        if (null != bn) {
     94            for (BigDecimal b : bn) {
     95                r = r.subtract((null == b ? BigDecimal.ZERO : b));
     96            }
     97        }
     98        return isZero ? (r.compareTo(BigDecimal.ZERO) == -1 ? BigDecimal.ZERO : r) : r;
     99    }
    100  
    101    /**
    102     * 整型的减法运算,小于0时返回0
    103     * @param b1
    104     * @param bn
    105     * @return
    106     */
    107    public static Integer safeSubtract(Integer b1, Integer... bn) {
    108        if (null == b1) {
    109            b1 = 0;
    110        }
    111        Integer r = b1;
    112        if (null != bn) {
    113            for (Integer b : bn) {
    114                r -= Optional.fromNullable(b).or(0);
    115            }
    116        }
    117        return null != r && r > 0 ? r : 0;
    118    }
    119  
    120    /**
    121     * 金额除法计算
    122     * @param b1
    123     * @param b2
    124     * @return
    125     */
    126    public static <T extends Number> BigDecimal safeDivide(T b1, T b2){
    127        return safeDivide(b1, b2, SCALE, BigDecimal.ZERO);
    128    }
    129    
    130    /**
    131     * 金额除法计算,返回指定位数的小数
    132     * @param b1
    133     * @param b2
    134     * @param scale 要保留的小数位
    135     * @return
    136     */
    137    public static <T extends Number> BigDecimal safeDivide(T b1, T b2, int scale){
    138        return safeDivide(b1, b2, scale, BigDecimal.ZERO);
    139    }
    140  
    141    /**
    142     * BigDecimal的除法运算封装,返回指定位数的小数,如果除数或者被除数为0,返回默认值
    143     * 默认返回小数位后2位,用于金额计算
    144     * @param b1
    145     * @param b2
    146     * @param scale 要保留的小数位
    147     * @param defaultValue 返回的默认值
    148     * @return
    149     */
    150    public static <T extends Number> BigDecimal safeDivide(T b1, T b2, int scale, T defaultValue) {
    151        if (null == b1 || null == b2) {
    152            return BigDecimal.valueOf(defaultValue.doubleValue());
    153        }
    154        try {
    155            return BigDecimal.valueOf(b1.doubleValue()).divide(BigDecimal.valueOf(b2.doubleValue()), scale, BigDecimal.ROUND_HALF_UP);
    156        } catch (Exception e) {
    157            return BigDecimal.valueOf(defaultValue.doubleValue());
    158        }
    159    }
    160  
    161    /**
    162     * BigDecimal的乘法运算封装
    163     * @param b1
    164     * @param b2
    165     * @return
    166     */
    167    public static <T extends Number> BigDecimal safeMultiply(T b1, T b2) {
    168        return safeMultiply(b1, b2,SCALE);
    169    }
    170    
    171    /**
    172     * BigDecimal的乘法运算封装,返回指定位数的小数
    173     * @param b1 
    174     * @param b2
    175     * @param scale 要保留的小数位
    176     * @return
    177     */
    178    public static <T extends Number> BigDecimal safeMultiply(T b1, T b2,int scale) {
    179        if (null == b1 || null == b2) {
    180            return BigDecimal.ZERO;
    181        }
    182        return BigDecimal.valueOf(b1.doubleValue()).multiply(BigDecimal.valueOf(b2.doubleValue())).setScale(scale, BigDecimal.ROUND_HALF_UP);
    183    }  
    184    
    185    /**
    186     * 保留N位小数,N位之后的四舍五入<br>
    187     *
    188     * @param current  : 当前需要改变的
    189     * @param position : 位数
    190     * @return
    191     */
    192    public static BigDecimal setScaleDown (BigDecimal current, int position ) {
    193        return current.setScale( position, BigDecimal.ROUND_HALF_UP );
    194    }
    195    
    196    /**
    197     * 判断是否是一个正数
    198     * <p>正数定义:比0大的实数叫正数[,正数前面常有一个符号"+",通常可以省略不写.</p>
    199     *
    200     * @param bigDecimal
    201     * @return
    202     */
    203    public static boolean isPositiveNumber ( BigDecimal bigDecimal ) {
    204        return bigDecimal.compareTo( BigDecimal.ZERO ) == 1;
    205    }
    206    
    207    /**
    208     * 判断是否不是一个正数
    209     *
    210     * @param bigDecimal
    211     * @return
    212     */
    213    public static boolean isNotPositiveNumber ( BigDecimal bigDecimal ) {
    214        return ! isPositiveNumber( bigDecimal );
    215    }
    216    
    217    /**
    218     * 判断两个参数是否相等
    219     *
    220     * @param before
    221     * @param after
    222     * @return
    223     */
    224    public static boolean isEqual ( BigDecimal before, BigDecimal after ) {
    225        return before.compareTo( after ) == 0;
    226    }
    227    
    228    
    229 }

      实际项目实践可以参考:https://www.cnblogs.com/summary-2017/p/9535671.html

    写博客是为了记住自己容易忘记的东西,另外也是对自己工作的总结,文章可以转载,无需版权。希望尽自己的努力,做到更好,大家一起努力进步!

    如果有什么问题,欢迎大家一起探讨,代码如有问题,欢迎各位大神指正!

  • 相关阅读:
    android -------- Data Binding的使用(二)
    牛客网-《剑指offer》-数值的整数次方[快速幂运算]
    牛客网-《剑指offer》-二进制中1的个数
    牛客网-《剑指offer》-矩形覆盖
    牛客网-《剑指offer》-变态跳台阶
    牛客网-《剑指offer》-跳台阶
    牛客网-《剑指offer》-斐波那契数列
    牛客网-《剑指offer》-旋转数组的最小数
    牛客网-《剑指offer》-用两个栈实现队列
    牛客网-《剑指offer》-重建二叉树
  • 原文地址:https://www.cnblogs.com/summary-2017/p/9535662.html
Copyright © 2011-2022 走看看