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 objectequals()checks both numeric value and scalecompareTo()checks numeric orderingsignum()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:
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:
signum() returns:
-1for negative numbers0for zero1for 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:
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:
A null-safe helper
If you check for zero often, put the rule in one method and make null handling explicit:
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.
Related reading
- How to check if IOException is Not-Enough-Disk-Space-Exception type?
- How to check if Receiver is registered in Android?
- How to check String in response body with mockMvc
- how to check the jdk version used to compile a .class file
- How to check two condition while using ConditionalOnProperty or ConditionalOnExpression
- How to check type of variable in Java?
- How to check whether a given string is valid JSON in Java
- How to clear gradle cache?
System design interview problems on this topic
- Design an API Rate Limiter: solution and walkthrough
- Design a Distributed Messaging System: solution and walkthrough
- Design an Online Payment Service: solution and walkthrough
- Design a Key Value Store: solution and walkthrough

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.