前言
需要计算的场景绕不过去会使用BigDecimal类,可频繁的判空让代码可读性下降也使代码冗余度增高,不判空又容易报空指针异常,而且有些场景下的计算,结果为空时需要返回null,所以也不可以将将BigDecimal类型的值都初始化为0,会让运算结果产生歧义。为解决上述痛点,于是整理了一个工具类供所有项目一起使用。
一.工具类支持功能
加减乘除、累加、倍数运算。Integer、Long、Float、Double、String、Object转为BigDecimal。除法四舍五入并默认保留20位小数点。BigDecimal转为String字符串,并禁止用科学计数法显示结果。小数点位数保留。多种数据类型间混合运算(如Double和Long转为BigDecimal运算)以上所有计算,支持所有入参为空时运算
二.工具类源码
import java
.math
.BigDecimal
;
public class DecimalUtils {
public static BigDecimal
add(BigDecimal x
, BigDecimal y
) {
if (x
== null
) {
return y
;
}
if (y
== null
) {
return x
;
}
return x
.add(y
);
}
public static BigDecimal
add(BigDecimal a
, BigDecimal b
, BigDecimal c
, BigDecimal d
) {
BigDecimal ab
= add(a
, b
);
BigDecimal cd
= add(c
, d
);
return add(ab
, cd
);
}
public static BigDecimal
accumulate(BigDecimal x
, BigDecimal result
) {
if (x
== null
) {
return result
;
}
if (result
== null
) {
result
= new BigDecimal("0");
}
return result
.add(x
);
}
public static BigDecimal
subtract(BigDecimal x
, BigDecimal y
) {
if (x
== null
|| y
== null
) {
return null
;
}
return x
.subtract(y
);
}
public static BigDecimal
multiply(BigDecimal x
, BigDecimal y
) {
if (x
== null
|| y
== null
) {
return null
;
}
return x
.multiply(y
);
}
public static BigDecimal
divide(BigDecimal x
, BigDecimal y
) {
if (x
== null
|| y
== null
|| y
.compareTo(BigDecimal
.ZERO
) == 0) {
return null
;
}
return stripTrailingZeros(x
.divide(y
, 20, BigDecimal
.ROUND_HALF_UP
));
}
public static String
toPlainString(BigDecimal x
) {
if (x
== null
) {
return null
;
}
return x
.toPlainString();
}
public static BigDecimal
scale(BigDecimal x
, int scale
) {
if (x
== null
) {
return null
;
}
return x
.setScale(scale
, BigDecimal
.ROUND_HALF_UP
);
}
public static BigDecimal
toBigDecimal(Integer x
) {
if (x
== null
) {
return null
;
}
return new BigDecimal(x
.toString());
}
public static BigDecimal
toBigDecimal(Long x
) {
if (x
== null
) {
return null
;
}
return new BigDecimal(x
.toString());
}
public static BigDecimal
toBigDecimal(Double x
) {
if (x
== null
) {
return null
;
}
return new BigDecimal(x
.toString());
}
public static BigDecimal
toBigDecimal(Float x
) {
if (x
== null
) {
return null
;
}
return new BigDecimal(x
.toString());
}
public static BigDecimal
toBigDecimal(String x
) {
if (x
== null
) {
return null
;
}
return new BigDecimal(x
);
}
public static BigDecimal
toBigDecimal(Object x
) {
if (x
== null
) {
return null
;
}
BigDecimal result
= null
;
try {
result
= new BigDecimal(x
.toString());
} catch (Exception e
) {
e
.printStackTrace();
}
return result
;
}
public static BigDecimal
multiple(BigDecimal x
, Integer multiple
) {
if (x
== null
|| multiple
== null
) {
return null
;
}
return DecimalUtils
.multiply(x
, toBigDecimal(multiple
));
}
public static BigDecimal
stripTrailingZeros(BigDecimal x
) {
if (x
== null
) {
return null
;
}
return x
.stripTrailingZeros();
}
}
三.如何使用
使用BigDecimal计算的时候,再也不用考虑判空、初始化数值了。代码简洁清爽了许多。
public static void main(String
[] args
) {
BigDecimal a
= new BigDecimal("100");
BigDecimal b
= new BigDecimal("3.14");
BigDecimal c1
= DecimalUtils
.add(a
, b
);
BigDecimal c2
= DecimalUtils
.subtract(a
, b
);
BigDecimal c3
= DecimalUtils
.multiply(a
, b
);
BigDecimal c4
= DecimalUtils
.subtract(a
, b
);
BigDecimal sum
= null
;
sum
= DecimalUtils
.accumulate(a
, sum
);
BigDecimal c5
= DecimalUtils
.scale(a
, 2);
BigDecimal c6
= DecimalUtils
.multiple(a
, 1000);
Object obj
= 123;
BigDecimal c7
= DecimalUtils
.toBigDecimal(obj
);
String str
= DecimalUtils
.toPlainString(a
);
BigDecimal mixCalc
= DecimalUtils
.divide(DecimalUtils
.toBigDecimal(1.23D),DecimalUtils
.toBigDecimal(1234567L
));
}
总结
BigDecimal是Java常用的计算工具,使用本工具类简化了你重复代码的编写,只需要关注业务逻辑即可,不必担心空指针的处理了。