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
🌐
Baeldung
baeldung.com › home › java › java numbers › check if bigdecimal value is zero
Check if BigDecimal Value Is Zero | Baeldung
January 16, 2024 - 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.
🌐
Tutorialspoint
tutorialspoint.com › home › java/math › bigdecimal compareto in java
BigDecimal compareTo in Java
September 1, 2008 - The following example shows the usage of math.BigDecimal.compareTo() method. package com.tutorialspoint; import java.math.*; public class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bg1, bg2; bg1 = new BigDecimal("10"); bg2 = new BigDecimal("20"); //create int object int res; res = bg1.compareTo(bg2); // compare bg1 with bg2 String str1 = "Both values are equal "; String str2 = "First Value is greater "; String str3 = "Second value is greater"; if( res == 0 ) System.out.println( str1 ); else if( res == 1 ) System.out.println( str2 ); else if( res == -1 ) System.out.println( str3 ); } } Let us compile and run the above program, this will produce the following result − ·
🌐
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 - -1: current BigDecimal is less than bg. This example shows that compareTo() compares numeric values only and ignores scale when determining which value is greater. Java ·
🌐
Igor's Techno Club
igorstechnoclub.com › java-bigdecimal
The Pitfalls of Comparing BigDecimals in Java | Igor's Techno Club
To avoid this pitfall and compare BigDecimals based on their numerical values, you have a couple of options: Use the compareTo() method: The compareTo() method compares the numerical values of BigDecimals, regardless of their scales.
🌐
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 ...
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - An ulp of a nonzero BigDecimal value is the positive distance between this value and the BigDecimal value next larger in magnitude with the same number of digits. An ulp of a zero value is numerically equal to 1 with the scale of this. The result is stored with the same scale as this so the ...
🌐
Java2Blog
java2blog.com › home › core java › bigdecimal › bigdecimal compareto
BigDecimal compareTo - Java2Blog
January 12, 2021 - Unlike equals method, if two BigDecimals have same value but different scale, compareTo method will return 0.It means compareTo method will consider them equal.
Find elsewhere
🌐
Oracle
docs.oracle.com › javase › 7 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 7 )
Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing ...
🌐
Medium
bterczynski.medium.com › comparing-bigdecimals-for-equality-71037fa2ee1d
Comparing BigDecimals for Equality | by Brian Terczynski | Medium
January 23, 2022 - 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 . Since they’re different, the == operation does not consider them to be equal. Instead, the Javadoc suggests that compareTo be used, because in this case I don’t actually care about the scale used to represent the number; I only care that the operation’s value is zero, regardless of what scale is used to represent it.
🌐
Coderanch
coderanch.com › t › 375912 › java › BigDecimal-compareTo
BigDecimal compareTo() problem (Java in General forum at Coderanch)
Java in General · Joe Busch · Greenhorn · Posts: 12 · posted 20 years ago · Number of slices to send: Optional 'thank-you' note: Send · 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.
🌐
Coderanch
coderanch.com › t › 449052 › java › finding-bigdecimal-greater
Best way of finding the bigdecimal is greater than 1 ? (Java in General forum at Coderanch)
The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
🌐
Oracle
docs.oracle.com › en › java › javase › 17 › docs › api › java.base › java › math › BigDecimal.html
BigDecimal (Java SE 17 & JDK 17)
January 20, 2026 - Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. Such values are in the same cohort. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, ...
🌐
Baeldung
baeldung.com › home › java › core java › bigdecimal equals() vs. compareto()
BigDecimal equals() vs. compareTo() | Baeldung
May 2, 2024 - In this context, we refer to the equality of two numbers regarding the compareTo() result being zero. Let’s check the following example of the compareTo() logic: static Stream<Arguments> decimalCompareToProvider() { return Stream.of( ...
🌐
Blogger
javablogx.blogspot.com › 2015 › 09 › how-to-test-if-bigdecimal-is-less-then.html
Java Examples: How to test if a BigDecimal is less then zero?
import java.math.*; public class BigDecimalTest { public static void main(String[] args) { BigDecimal bd = new BigDecimal(-1.5); if (bd.compareTo(BigDecimal.ZERO) < 0) { System.out.println(bd + " is less then zero"); } } }
🌐
Educative
educative.io › answers › how-to-compare-two-bigdecimals-in-java
How to compare two BigDecimals in Java
A BigDecimal consists of a 32-bit integer scale; it is used to handle very large and very small floating-point numbers. Java provides the built-in function compareTo() which compares the two BigDecimals.
🌐
Coderanch
coderanch.com › t › 710858 › java › BigDecimal-comparison-values
BigDecimal comparison between 2 values w and w/o 0 [Solved] (Beginning Java forum at Coderanch)
I think that's the best solution - check if bigDecimalA.compareTo(bigDecimalB) == 0, if true that means they're equal. There are other things you could do, like check if check if bigDecimalA.doubleValue() == bigDecimalB.doubleValue(). That will give you the right answer as long as your numbers ...
🌐
Intellipaat
intellipaat.com › home › blog › compare if bigdecimal is greater than zero
Compare if BigDecimal is greater than zero - Intellipaat Blog
July 18, 2025 - In Java, To compare whether a BigDecimal value is greater than zero or not, we have to use the compareTo() function. In primitive data types we can do this comparison with the help of relational operators like: (<, >, <=, >=).