Java
BigDecimal
Programming
Variable Comparison
Zero Check

How to check if a BigDecimal is zero in Java (BigDecimal.ZERO)

Codemia Knowledge Hub ·

Use value.compareTo(BigDecimal.ZERO) == 0. It compares the numeric value, so 0, 0.0 and 0.00 all count as zero. value.signum() == 0 is an equally correct zero-specific check. Do not use == (object identity) or equals() (which also compares scale).

Four kinds of comparison on BigDecimal

BigDecimal has more than one notion of equality, which is the source of the confusion:

  • == checks whether two references point to the same object
  • equals() checks both numeric value and scale
  • compareTo() checks numeric ordering
  • signum() checks only whether the number is negative, zero or positive

Most zero checks want numeric equality, which is what compareTo() and signum() give you.

compareTo for numeric equality

compareTo() ignores scale differences:

java
1import java.math.BigDecimal;
2
3public class BigDecimalCompareDemo {
4    public static void main(String[] args) {
5        BigDecimal a = new BigDecimal("0");
6        BigDecimal b = new BigDecimal("0.00");
7        BigDecimal c = new BigDecimal("5.25");
8
9        System.out.println(a.compareTo(BigDecimal.ZERO) == 0); // true
10        System.out.println(b.compareTo(BigDecimal.ZERO) == 0); // true
11        System.out.println(c.compareTo(BigDecimal.ZERO) == 0); // false
12    }
13}

This is the usual recommendation because it handles the general case and also works for less-than and greater-than checks.

signum() for a direct zero check

If the only question is "is this zero", signum() reads clearly:

java
1import java.math.BigDecimal;
2
3public class BigDecimalSignumDemo {
4    public static void main(String[] args) {
5        BigDecimal value = new BigDecimal("0.000");
6        System.out.println(value.signum() == 0); // true
7    }
8}

signum() returns:

  • -1 for negative numbers
  • 0 for zero
  • 1 for positive numbers

It is concise, but compareTo() is better if the code also needs ordering logic.

Why equals() fails for 0.00

equals() includes scale, so numerically equal values can be unequal:

java
1import java.math.BigDecimal;
2
3public class BigDecimalEqualsDemo {
4    public static void main(String[] args) {
5        BigDecimal x = new BigDecimal("0");
6        BigDecimal y = new BigDecimal("0.00");
7
8        System.out.println(x.equals(BigDecimal.ZERO)); // true
9        System.out.println(y.equals(BigDecimal.ZERO)); // false
10    }
11}

This is intentional, since scale sometimes matters in business logic. But if every numeric zero should count as zero, equals() is too strict.

Why == is wrong

BigDecimal is an object, so == compares references. Two separate zero-valued instances are usually different objects:

java
1import java.math.BigDecimal;
2
3public class BigDecimalReferenceDemo {
4    public static void main(String[] args) {
5        BigDecimal value = new BigDecimal("0");
6
7        System.out.println(value == BigDecimal.ZERO); // false
8        System.out.println(value.compareTo(BigDecimal.ZERO) == 0); // true
9    }
10}

A null-safe helper

If you check for zero often, put the rule in one method and make null handling explicit:

java
1import java.math.BigDecimal;
2
3public final class DecimalUtils {
4    private DecimalUtils() {}
5
6    public static boolean isZero(BigDecimal value) {
7        return value != null && value.compareTo(BigDecimal.ZERO) == 0;
8    }
9}

Calling compareTo() on a null reference throws NullPointerException, so decide up front how null should be treated. Also be aware that creating a BigDecimal from a double can introduce precision surprises elsewhere in the calculation.


Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms