Java的BigDecimal,对运算封装

  1. 添加maven依赖
  2. <dependency>   
  3.  <groupId>com.google.guava</groupId>  
  4.  <artifactId>guava</artifactId>  
  5.  <version>18.0</version>
  6. </dependency>  
  7. import java.math.BigDecimal;  

  8.   

  9. public class NumberArithmeticUtils {  

  10.       

  11.       

  12.     /** 

  13.      * BigDecimal的加法运算封装 

  14.      * @author : shijing 

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

  16.      * @param b1 

  17.      * @param bn 

  18.      * @return 

  19.      */  

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

  21.        if (null == b1) {  

  22.            b1 = BigDecimal.ZERO;  

  23.        }  

  24.        if (null != bn) {  

  25.            for (BigDecimal b : bn) {  

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

  27.            }  

  28.        }  

  29.        return b1;  

  30.    }  

  31.   

  32.    /** 

  33.     * Integer加法运算的封装 

  34.     * @author : shijing 

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

  36.     * @param b1   第一个数 

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

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

  39.     * @return 

  40.     */  

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

  42.        if (null == b1) {  

  43.            b1 = 0;  

  44.        }  

  45.        Integer r = b1;  

  46.        if (null != bn) {  

  47.            for (Integer b : bn) {  

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

  49.            }  

  50.        }  

  51.        return r > 0 ? r : 0;  

  52.    }  

  53.   

  54.    /** 

  55.     * 计算金额方法 

  56.     * @author : shijing 

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

  58.     * @param b1 

  59.     * @param bn 

  60.     * @return 

  61.     */  

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

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

  64.    }  

  65.   

  66.    /** 

  67.     * BigDecimal的安全减法运算 

  68.     * @author : shijing 

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

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

  71.     * @param b1        被减数 

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

  73.     * @return 

  74.     */  

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

  76.        if (null == b1) {  

  77.            b1 = BigDecimal.ZERO;  

  78.        }  

  79.        BigDecimal r = b1;  

  80.        if (null != bn) {  

  81.            for (BigDecimal b : bn) {  

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

  83.            }  

  84.        }  

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

  86.    }  

  87.   

  88.    /** 

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

  90.     * @author : shijing 

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

  92.     * @param b1 

  93.     * @param bn 

  94.     * @return 

  95.     */  

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

  97.        if (null == b1) {  

  98.            b1 = 0;  

  99.        }  

  100.        Integer r = b1;  

  101.        if (null != bn) {  

  102.            for (Integer b : bn) {  

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

  104.            }  

  105.        }  

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

  107.    }  

  108.   

  109.    /** 

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

  111.     * @author : shijing 

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

  113.     * @param b1 

  114.     * @param b2 

  115.     * @return 

  116.     */  

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

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

  119.    }  

  120.   

  121.    /** 

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

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

  124.     * @author : shijing 

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

  126.     * @param b1 

  127.     * @param b2 

  128.     * @param defaultValue 

  129.     * @return 

  130.     */  

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

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

  133.            return defaultValue;  

  134.        }  

  135.        try {  

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

  137.        } catch (Exception e) {  

  138.            return defaultValue;  

  139.        }  

  140.    }  

  141.   

  142.    /** 

  143.     * BigDecimal的乘法运算封装 

  144.     * @author : shijing 

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

  146.     * @param b1 

  147.     * @param b2 

  148.     * @return 

  149.     */  

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

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

  152.            return BigDecimal.ZERO;  

  153.        }  

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

  155.    }  

  156.   

  157. }  

猜你喜欢

转载自www.cnblogs.com/dinghaoran/p/11050239.html