Check if BigDecimal is an integer in Java

JavaBigdecimal

Java Problem Overview


What is 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 possible.

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.

Java Solutions


Solution 1 - Java

EDIT: As of Java 8, stripTrailingZeroes() now accounts for zero

https://stackoverflow.com/questions/34414785/bigdecimal-striptrailingzeros-doesnt-work-for-zero

So

private boolean isIntegerValue(BigDecimal bd) {
  return bd.stripTrailingZeros().scale() <= 0;
}

Is perfectly fine now.


If you use the scale() and stripTrailingZeros() solution mentioned in some of the answers you should pay attention to zero. Zero always is an integer no matter what scale it has, and stripTrailingZeros() does not alter the scale of a zero BigDecimal.

So you could do something like this:

private boolean isIntegerValue(BigDecimal bd) {
  return bd.signum() == 0 || bd.scale() <= 0 || bd.stripTrailingZeros().scale() <= 0;
}

Solution 2 - Java

Depending on the source/usage of your BigDecimal values it might be faster to check if the scale <= 0 first. If it is, then it's definitely an integer value in the mathematical sense. If it is >0, then it could still be an integer value and the more expensive test would be needed.

Solution 3 - Java

Divide the number by 1 and check for a remainder. Any whole number should always have a remainder of 0 when divided by 1.

public boolean isWholeNumber(BigDecimal number) {
    return number.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0;
}

Solution 4 - Java

You can use this (just summarizing from other answers):

private boolean isIntegerValue(BigDecimal bd) {
  return bd.stripTrailingZeros().scale() <= 0;
}

Solution 5 - Java

This can be done the same way that someone would check if a double is an integer, performing % 1 == 0. This is how it would look for a BigDecimal value.

public static boolean isIntegerValue(BigDecimal bd){
  return bd.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0;
}

Solution 6 - Java

One possiblity should be to check if scale() is zero or negative. In that case the BigDecimal should have no digits after the decimal point and the number should be a mathematical integer if I understand your question correctly.

Update: If positive, it could still be an integer, but you cannot spare extra object creations for further in-depth checks in that case. An example for such a case is given at the stripTrailingZeros() method javadoc (thanks Joachim for the hint in his answer).

Solution 7 - Java

This is the cleanest I've come up with.

public static boolean isWhole(BigDecimal bigDecimal) {
    return bigDecimal.setScale(0, RoundingMode.HALF_UP).compareTo(bigDecimal) == 0;
}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdamskiView Question on Stackoverflow
Solution 1 - JavatobiView Answer on Stackoverflow
Solution 2 - JavaJoachim SauerView Answer on Stackoverflow
Solution 3 - JavaStPatrickView Answer on Stackoverflow
Solution 4 - JavaemischView Answer on Stackoverflow
Solution 5 - JavaScott ArmstrongView Answer on Stackoverflow
Solution 6 - JavaKosi2801View Answer on Stackoverflow
Solution 7 - JavaIanView Answer on Stackoverflow