确定 a 是否是数学意义上的整数值的有效方法是什么?BigDecimal
目前我有以下代码:
private boolean isIntegerValue(BigDecimal bd) {
boolean ret;
try {
bd.toBigIntegerExact();
ret = true;
} catch (ArithmeticException ex) {
ret = false;
}
return ret;
}
...但如果可能的话,希望避免对象创建开销。
以前我使用bd.longValueExact()
which 将避免在内部使用其紧凑表示时创建对象BigDecimal
,但如果值太大而无法放入 long 中,显然会失败。