Check if BigDecimal is integer value

Posted by Adamski on Stack Overflow See other posts from Stack Overflow or by Adamski
Published on 2009-07-03T11:28:16Z Indexed on 2010/06/06 18:32 UTC
Read the original article Hit count: 343

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about java

Related posts about bigdecimal