Can anyone recommend an efficient way of determining whether a BigDecimal
is an integer value in the mathematical sense?
At present I have the following code:
private boolean isIntegerValue(BigDecimal bd) {
boolean ret;
try {
bd.toBigIntegerExact();
ret = true;
} catch (ArithmeticException ex) {
ret = false;
}
return ret;
}
... but would like to avoid the object creation overhead if necessary. Previously I was using bd.longValueExact()
which would avoid creating an object if the BigDecimal
was using its compact representation internally, but obviously would fail if the value was too big to fit into a long.
Any help appreciated.