BigDecimal
Java
Programming
Comparison Operators
Zero Comparison

Check if a Java BigDecimal is greater than zero

Codemia Knowledge Hub ·

Use value.compareTo(BigDecimal.ZERO) > 0, or value.signum() > 0 if you only care about the sign. Relational operators such as > do not work because BigDecimal is an object, not a primitive, and equals() is the wrong tool because it also compares scale.

compareTo against BigDecimal.ZERO

The standard check compares the value with BigDecimal.ZERO:

java
1import java.math.BigDecimal;
2
3public class Main {
4    public static void main(String[] args) {
5        BigDecimal value = new BigDecimal("3.50");
6
7        if (value.compareTo(BigDecimal.ZERO) > 0) {
8            System.out.println("greater than zero");
9        }
10    }
11}

compareTo returns:

  • a negative number if the left value is smaller
  • 0 if the values are numerically equal
  • a positive number if the left value is greater

So compareTo(BigDecimal.ZERO) > 0 is the normal test for a positive value.

signum() for a pure sign check

If the question is only about sign, signum() is more direct:

java
1import java.math.BigDecimal;
2
3public class Main {
4    public static void main(String[] args) {
5        BigDecimal value = new BigDecimal("3.50");
6
7        if (value.signum() > 0) {
8            System.out.println("greater than zero");
9        }
10    }
11}

signum() returns:

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

Why equals() is not a numeric comparison

equals() also considers scale, while compareTo() compares only the numeric value:

java
1import java.math.BigDecimal;
2
3public class Main {
4    public static void main(String[] args) {
5        BigDecimal a = new BigDecimal("0.0");
6        BigDecimal b = new BigDecimal("0.00");
7
8        System.out.println(a.equals(b));
9        System.out.println(a.compareTo(b) == 0);
10    }
11}

The first line prints false because the scale differs. The second prints true because both are numerically zero. Mixing the two without knowing this creates subtle financial and validation bugs, so use compareTo() or signum() for zero and ordering checks.

Use the BigDecimal.ZERO constant

Compare with BigDecimal.ZERO rather than creating new BigDecimal("0") each time:

java
if (amount.compareTo(BigDecimal.ZERO) > 0) {
    System.out.println("positive");
}

The constant is clearer, avoids needless object creation and states the intent.

Handling null

Neither compareTo() nor signum() handles null; calling either on a null reference throws NullPointerException. If the value may be absent, check for that explicitly:

java
1import java.math.BigDecimal;
2
3public class Main {
4    static boolean isPositive(BigDecimal value) {
5        return value != null && value.compareTo(BigDecimal.ZERO) > 0;
6    }
7
8    public static void main(String[] args) {
9        System.out.println(isPositive(new BigDecimal("1.25")));
10        System.out.println(isPositive(null));
11    }
12}

Consistency in financial code

This check appears constantly in billing, ledger, settlement and validation code. Since BigDecimal is usually chosen for correctness-sensitive work, keep the comparison style consistent. Teams often standardize on compareTo(BigDecimal.ZERO) for ordering checks and reserve equals() for cases where scale-sensitive equality is actually intended.


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