术语"偶数"通常不适用于十进制数,但如果您只是想测试小数的十进制表示的最后一位数是否包含2,4,6或8,那么您可以执行以下操作:
BigDecimal decimal = new BigDecimal("123.456");
BigInteger integer = decimal.stripTrailingZeros().unscaledValue();
boolean isEven = !integer.testBit(0);
Run Code Online (Sandbox Code Playgroud)
此方法可避免在将BigDecimal转换为int,long或double时丢失任何精度或导致任何溢出.