Use compareTo(BigDecimal.ZERO) instead of equals():

if (price.compareTo(BigDecimal.ZERO) == 0) // see below

Comparing with the BigDecimal constant BigDecimal.ZERO avoids having to construct a new BigDecimal(0) every execution.

FYI, BigDecimal also has constants BigDecimal.ONE and BigDecimal.TEN for your convenience.


Note!

The reason you can't use BigDecimal#equals() is that it takes scale into consideration:

new BigDecimal("0").equals(BigDecimal.ZERO) // true
new BigDecimal("0.00").equals(BigDecimal.ZERO) // false!

so it's unsuitable for a purely numeric comparison. However, BigDecimal.compareTo() doesn't consider scale when comparing:

new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0 // true
new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0 // true
Answer from Bohemian on Stack Overflow
🌐
Medium
bterczynski.medium.com › comparing-bigdecimals-for-equality-71037fa2ee1d
Comparing BigDecimals for Equality | by Brian Terczynski | Medium
January 23, 2022 - Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method). So when I looked at the actual value of BigDecimal.ZERO in the debugger, it came up as this: Note that one field in particular is different than the above: the scale. It’s 0 in this case, whereas above it’s 1 ...
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal compareto in java
BigDecimal compareTo in Java
September 1, 2008 - Following is the declaration for java.math.BigDecimal.compareTo() method. ... This method returns -1 if the BigDecimal is less than val, 1 if the BigDecimal is greater than val and 0 if the BigDecimal is equal to val
🌐
Baeldung
baeldung.com › home › java › java numbers › check if bigdecimal value is zero
Check if BigDecimal Value Is Zero | Baeldung
January 16, 2024 - On the other side, the BigDecimal.ZERO constant has the value zero and a scale of zero, too. So, when we check “0 equals 0.0000“, the equals method returns false. Therefore, we need to find a way only to compare two BigDecimal objects’ values but ignore their scales. Next, let’s see a couple of approaches to solve the problem. The BigDecimal class implements the Comparable interface. So, we can use the compareTo method to compare two BigDecimal objects’ values.
🌐
Java2Blog
java2blog.com › home › core java › bigdecimal › bigdecimal compareto
BigDecimal compareTo - Java2Blog
January 12, 2021 - In this post,we will see about Bigdecimal‘s compareTo method.This method is used to compare two BigDecimals. CompareTo method returns -1,0,1 -1 : First BigDeicmal is less than second BigDecimal 0 : First BigDecimal is equal to second BigDecimal 1 : First BigDecimal is greater than second ...
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.compareTo java code examples | Tabnine
public BigDecimal eval(List<? extends ... x = (BigDecimal) parameters.get(0); if (x.compareTo(BigDecimal.ZERO) == 0) { return new BigDecimal(0); } if (x.signum() < 0) { throw new ExpressionException("Argument to SQRT() function ...
🌐
Kotlin Discussions
discuss.kotlinlang.org › t › bigdecimal-comparison › 1828
BigDecimal comparison? - Kotlin Discussions
June 28, 2016 - (BigDecimal(“0E-8”) == BigDecimal.ZERO) - false (BigDecimal(“0E-8”).compareTo(BigDecimal.ZERO) == 0) - true (BigDecimal(“0E-8”) == BigDecimal.ZERO.setScale(8)) - true Is it a bug?
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - a numerically equal BigDecimal with any trailing zeros removed. Since: 1.5 · public int compareTo(BigDecimal val) Compares this BigDecimal with the specified BigDecimal. Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this ...
Find elsewhere
🌐
Microsoft Learn
learn.microsoft.com › en-us › dotnet › api › java.math.bigdecimal.compareto
BigDecimal.CompareTo(BigDecimal) Method (Java.Math) | Microsoft Learn
[<Android.Runtime.Register("compareTo", ... is to be compared. Int32 · -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val....
🌐
Baeldung
baeldung.com › home › java › core java › bigdecimal equals() vs. compareto()
BigDecimal equals() vs. compareTo() | Baeldung
May 2, 2024 - The compareTo() method treats the numbers 0.1, 0.10, 0.100, and 0.1000 as the same. It ignores trailing zeros, which is quite reasonable and expected in most cases. While it’s highly recommended to respect the consistency between compareTo() and equals(), it isn’t a strict rule. Here’s the snipped from the documentation of the Comparable interface: It is strongly recommended (though it isn’t required) that natural orderings be consistent with equals. The BigDecimal class doesn’t follow this recommendation.
🌐
Baeldung
baeldung.com › home › java › java numbers › bigdecimal.zero vs. new bigdecimal(0)
BigDecimal.ZERO vs. new BigDecimal(0) | Baeldung
March 17, 2024 - Let’s say we have two BigDecimal objects, bd1 and bd2. If bd1.compareTo(bd2) == 0, it only indicates the two BigDecimals are equal in value.
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-compareto-function-in-java
BigDecimal compareTo() Function in Java - GeeksforGeeks
June 25, 2018 - Since 4743.00001 is less than 4743.0008, the method returns -1, indicating b1 < b2. Note : compareTo() compares only the value, not the scale.This behavior is different from equals(), which considers both value and scale.
🌐
GitHub
github.com › ac183 › equalsverifier › blob › 156b1014cbc1db2a7eab2a276b0b2e1f73afd7ff › docs › _manual › 12-bigdecimal-compareto.md
equalsverifier/docs/_manual/12-bigdecimal-compareto.md at 156b1014cbc1db2a7eab2a276b0b2e1f73afd7ff · ac183/equalsverifier
// prints true - zero is the same as zero System.out.println(zero.compareTo(alsoZero) == 0); // prints false - zero is not the same as zero System.out.println(zero.equals(alsoZero)); {% endhighlight %} This is because BigDecimal can have multiple representations of the same value. It uses an unscaled value and a scale so, for example, the value of 1 can be represented as unscaled value 1 with scale of 0 (the number of places after the decimal point) or as unscaled value 10 with scale of 1 resolving to 1.0.
Author   ac183
🌐
Coderanch
coderanch.com › t › 375912 › java › BigDecimal-compareTo
BigDecimal compareTo() problem (Java in General forum at Coderanch)
I have 3 BigDecimal variables: a, b and c. I then do the following test: if( a.add(b).compareTo(c) == 0 ) a = 49.9900000000000001827363995 b = 50.01 c = 100 This sum does not equal zero. How do I set the scale/rounding for all three BigDecimals to get the desired result to equal zero?
🌐
Igor's Techno Club
igorstechnoclub.com › java-bigdecimal
The Pitfalls of Comparing BigDecimals in Java | Igor's Techno Club
It returns 0 if the numerical values are equal. Here's an example: BigDecimal value1 = new BigDecimal("1.23"); BigDecimal value2 = new BigDecimal("1.230"); System.out.println(value1.compareTo(value2)); // Output: 0
🌐
Educative
educative.io › answers › how-to-compare-two-bigdecimals-in-java
How to compare two BigDecimals in Java
The comparison can not be done using the >, < or = operators as these operators can only be used for the primitive data types like int, long and double. 1: when the first BigDecimal is greater than the second ...
🌐
EqualsVerifier
jqno.nl › equalsverifier › errormessages › bigdecimal-equality
BigDecimal equality - EqualsVerifier
January 26, 2026 - This means standard equals and hashCode can be used. For example, your class ensures all variants of 1 such as 1.0, 1.00, etc are converted to 1 in its constructor. ... class Foo { ... Foo(BigDecimal bdField) { // Now if bdField is final it will only have instances // that are equal when compareTo ...