BigDecimal 可以实现对浮点数的运算,不会造成精度丢失。
通常情况下,大部分需要浮点数精确运算结果的业务场景(比如涉及到钱的场景)都是通过 BigDecimal 来做的。
《阿里巴巴 Java 开发手册》中提到:浮点数之间的等值判断,基本数据类型不能用 == 来比较,包装数据类型不能用 equals 来判断。

具体原因我们在上面已经详细介绍了,这里就不多提了。
想要解决浮点数运算精度丢失这个问题,可以直接使用 BigDecimal 来定义浮点数的值,然后再进行浮点数的运算操作即可。
- BigDecimal a = new BigDecimal("1.0");
- BigDecimal b = new BigDecimal("0.9");
- BigDecimal c = new BigDecimal("0.8");
-
- BigDecimal x = a.subtract(b);
- BigDecimal y = b.subtract(c);
-
- System.out.println(x.compareTo(y));// 0
我们在使用 BigDecimal 时,为了防止精度丢失,推荐使用它的BigDecimal(String val)构造方法或者 BigDecimal.valueOf(double val) 静态方法来创建对象。
《阿里巴巴 Java 开发手册》对这部分内容也有提到,如下图所示。

add 方法用于将两个 BigDecimal 对象相加,subtract 方法用于将两个 BigDecimal 对象相减。multiply 方法用于将两个 BigDecimal 对象相乘,divide 方法用于将两个 BigDecimal 对象相除。
- BigDecimal a = new BigDecimal("1.0");
- BigDecimal b = new BigDecimal("0.9");
- System.out.println(a.add(b));// 1.9
- System.out.println(a.subtract(b));// 0.1
- System.out.println(a.multiply(b));// 0.90
- System.out.println(a.divide(b));// 无法除尽,抛出 ArithmeticException 异常
- System.out.println(a.divide(b, 2, RoundingMode.HALF_UP));// 1.11
这里需要注意的是,在我们使用 divide 方法的时候尽量使用 3 个参数版本,并且RoundingMode 不要选择 UNNECESSARY,否则很可能会遇到 ArithmeticException(无法除尽出现无限循环小数的时候),其中 scale 表示要保留几位小数,roundingMode 代表保留规则。
- public BigDecimal divide(BigDecimal divisor, int scale, RoundingMode roundingMode) {
- return divide(divisor, scale, roundingMode.oldMode);
- }
保留规则非常多,这里列举几种:
- public enum RoundingMode {
- // 2.5 -> 3 , 1.6 -> 2
- // -1.6 -> -2 , -2.5 -> -3
- UP(BigDecimal.ROUND_UP),
- // 2.5 -> 2 , 1.6 -> 1
- // -1.6 -> -1 , -2.5 -> -2
- DOWN(BigDecimal.ROUND_DOWN),
- // 2.5 -> 3 , 1.6 -> 2
- // -1.6 -> -1 , -2.5 -> -2
- CEILING(BigDecimal.ROUND_CEILING),
- // 2.5 -> 2 , 1.6 -> 1
- // -1.6 -> -2 , -2.5 -> -3
- FLOOR(BigDecimal.ROUND_FLOOR),
- // 2.5 -> 3 , 1.6 -> 2
- // -1.6 -> -2 , -2.5 -> -3
- HALF_UP(BigDecimal.ROUND_HALF_UP),
- //......
- }
a.compareTo(b) : 返回 -1 表示 a 小于 b,0 表示 a 等于 b , 1 表示 a 大于 b。
- BigDecimal a = new BigDecimal("1.0");
- BigDecimal b = new BigDecimal("0.9");
- System.out.println(a.compareTo(b));// 1
通过 setScale方法设置保留几位小数以及保留规则。保留规则有挺多种,不需要记,IDEA 会提示。
- BigDecimal m = new BigDecimal("1.255433");
- BigDecimal n = m.setScale(3,RoundingMode.HALF_DOWN);
- System.out.println(n);// 1.255
《阿里巴巴 Java 开发手册》中提到:

BigDecimal 使用 equals() 方法进行等值比较出现问题的代码示例:
- BigDecimal a = new BigDecimal("1");
- BigDecimal b = new BigDecimal("1.0");
- System.out.println(a.equals(b));//false
这是因为 equals() 方法不仅仅会比较值的大小(value)还会比较精度(scale),而 compareTo() 方法比较的时候会忽略精度。
1.0 的 scale 是 1,1 的 scale 是 0,因此 a.equals(b) 的结果是 false。

compareTo() 方法可以比较两个 BigDecimal 的值,如果相等就返回 0,如果第 1 个数比第 2 个数大则返回 1,反之返回-1。
- BigDecimal a = new BigDecimal("1");
- BigDecimal b = new BigDecimal("1.0");
- System.out.println(a.compareTo(b));//0
浮点数没有办法用二进制精确表示,因此存在精度丢失的风险。
不过,Java 提供了BigDecimal 来操作浮点数。BigDecimal 的实现利用到了 BigInteger (用来操作大整数), 所不同的是 BigDecimal 加入了小数位的概念。