日常对于金额计算,应该都是用的BigDecimal,  可是苦于没有好的工具类方法,现在贡献一个我正在用的对于数字计算的工具类,项目中就是用的这个,简单粗暴好用,话不多说,代码奉上(该工具类需要引入google的一个jar  ,com.google.common.base.Optional,具体maven引入看文章末尾):



[java] view plain copy

  1. import java.math.BigDecimal;  

  2.   

  3. public class NumberArithmeticUtils {  

  4.       

  5.       

  6.     /** 

  7.      * BigDecimal的加法运算封装 

  8.      * @author : shijing 

  9.      * 2017年3月23日下午4:53:21 

  10.      * @param b1 

  11.      * @param bn 

  12.      * @return 

  13.      */  

  14.    public static BigDecimal safeAdd(BigDecimal b1, BigDecimal... bn) {  

  15.        if (null == b1) {  

  16.            b1 = BigDecimal.ZERO;  

  17.        }  

  18.        if (null != bn) {  

  19.            for (BigDecimal b : bn) {  

  20.                b1 = b1.add(null == b ? BigDecimal.ZERO : b);  

  21.            }  

  22.        }  

  23.        return b1;  

  24.    }  

  25.   

  26.    /** 

  27.     * Integer加法运算的封装 

  28.     * @author : shijing 

  29.     * 2017年3月23日下午4:54:08 

  30.     * @param b1   第一个数 

  31.     * @param bn   需要加的加法数组 

  32.     * @注 : Optional  是属于com.google.common.base.Optional<T> 下面的class 

  33.     * @return 

  34.     */  

  35.    public static Integer safeAdd(Integer b1, Integer... bn) {  

  36.        if (null == b1) {  

  37.            b1 = 0;  

  38.        }  

  39.        Integer r = b1;  

  40.        if (null != bn) {  

  41.            for (Integer b : bn) {  

  42.                r += Optional.fromNullable(b).or(0);  

  43.            }  

  44.        }  

  45.        return r > 0 ? r : 0;  

  46.    }  

  47.   

  48.    /** 

  49.     * 计算金额方法 

  50.     * @author : shijing 

  51.     * 2017年3月23日下午4:53:00 

  52.     * @param b1 

  53.     * @param bn 

  54.     * @return 

  55.     */  

  56.    public static BigDecimal safeSubtract(BigDecimal b1, BigDecimal... bn) {  

  57.        return safeSubtract(true, b1, bn);  

  58.    }  

  59.   

  60.    /** 

  61.     * BigDecimal的安全减法运算 

  62.     * @author : shijing 

  63.     * 2017年3月23日下午4:50:45 

  64.     * @param isZero  减法结果为负数时是否返回0,true是返回0(金额计算时使用),false是返回负数结果 

  65.     * @param b1        被减数 

  66.     * @param bn        需要减的减数数组 

  67.     * @return 

  68.     */  

  69.    public static BigDecimal safeSubtract(Boolean isZero, BigDecimal b1, BigDecimal... bn) {  

  70.        if (null == b1) {  

  71.            b1 = BigDecimal.ZERO;  

  72.        }  

  73.        BigDecimal r = b1;  

  74.        if (null != bn) {  

  75.            for (BigDecimal b : bn) {  

  76.                r = r.subtract((null == b ? BigDecimal.ZERO : b));  

  77.            }  

  78.        }  

  79.        return isZero ? (r.compareTo(BigDecimal.ZERO) == -1 ? BigDecimal.ZERO : r) : r;  

  80.    }  

  81.   

  82.    /** 

  83.     * 整型的减法运算,小于0时返回0 

  84.     * @author : shijing 

  85.     * 2017年3月23日下午4:58:16 

  86.     * @param b1 

  87.     * @param bn 

  88.     * @return 

  89.     */  

  90.    public static Integer safeSubtract(Integer b1, Integer... bn) {  

  91.        if (null == b1) {  

  92.            b1 = 0;  

  93.        }  

  94.        Integer r = b1;  

  95.        if (null != bn) {  

  96.            for (Integer b : bn) {  

  97.                r -= Optional.fromNullable(b).or(0);  

  98.            }  

  99.        }  

  100.        return  != r && r > 0 ? r : 0;  

  101.    }  

  102.   

  103.    /** 

  104.     * 金额除法计算,返回2位小数(具体的返回多少位大家自己看着改吧) 

  105.     * @author : shijing 

  106.     * 2017年3月23日下午5:02:17 

  107.     * @param b1 

  108.     * @param b2 

  109.     * @return 

  110.     */  

  111.    public static <T extends Number> BigDecimal safeDivide(T b1, T b2){  

  112.        return safeDivide(b1, b2, BigDecimal.ZERO);  

  113.    }  

  114.   

  115.    /** 

  116.     * BigDecimal的除法运算封装,如果除数或者被除数为0,返回默认值 

  117.     * 默认返回小数位后2位,用于金额计算 

  118.     * @author : shijing 

  119.     * 2017年3月23日下午4:59:29 

  120.     * @param b1 

  121.     * @param b2 

  122.     * @param defaultValue 

  123.     * @return 

  124.     */  

  125.    public static <T extends Number> BigDecimal safeDivide(T b1, T b2, BigDecimal defaultValue) {  

  126.        if (null == b1 ||  null == b2) {  

  127.            return defaultValue;  

  128.        }  

  129.        try {  

  130.            return BigDecimal.valueOf(b1.doubleValue()).divide(BigDecimal.valueOf(b2.doubleValue()), 2, BigDecimal.ROUND_HALF_UP);  

  131.        } catch (Exception e) {  

  132.            return defaultValue;  

  133.        }  

  134.    }  

  135.   

  136.    /** 

  137.     * BigDecimal的乘法运算封装 

  138.     * @author : shijing 

  139.     * 2017年3月23日下午5:01:57 

  140.     * @param b1 

  141.     * @param b2 

  142.     * @return 

  143.     */  

  144.    public static <T extends Number> BigDecimal safeMultiply(T b1, T b2) {  

  145.        if (null == b1 ||  null == b2) {  

  146.            return BigDecimal.ZERO;  

  147.        }  

  148.        return BigDecimal.valueOf(b1.doubleValue()).multiply(BigDecimal.valueOf(b2.doubleValue())).setScale(2, BigDecimal.ROUND_HALF_UP);  

  149.    }  

  150.   

  151. }  


Optional所在的jar以及版本:guava-18.0.ar


pom.xml配置:


[html] view plain copy

  1. <!-- https://mvnrepository.com/artifact/com.google.guava/guava -->  

  2. <dependency>  

  3.     <groupId>com.google.guava</groupId>  

  4.     <artifactId>guava</artifactId>  

  5.     <version>18.0</version>  

  6. </dependency>