Build a comparator:

Comparator<BigDecimal> c = Comparator.nullsFirst(Comparator.naturalOrder());

(Or nullsLast, it doesn't matter if you're only ever comparing to zero).

Then:

if (c.compare(first, second) != 0) {
  // ...
}
Answer from Andy Turner on Stack Overflow
🌐
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", "(Ljava/math/BigDecimal;)I", "GetCompareTo_Ljava_math_BigDecimal_Handler")>] abstract member CompareTo : Java.Math.BigDecimal -> int override this.CompareTo : Java.Math.BigDecimal -> int · val · BigDecimal · BigDecimal to which this BigDecimal is to be compared. Int32 · -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val. Attributes · RegisterAttribute · NullPointerException ·
🌐
Baeldung
baeldung.com › home › java › java numbers › check if bigdecimal value is zero
Check if BigDecimal Value Is Zero | Baeldung
January 16, 2024 - When we run the test, it passes. So, we’ve solved the problem using the compareTo method. The BigDeicmal class provides the signum method to tell if the given BigDecimal object’s value is negative (-1), zero (0), or positive (1).
🌐
Oracle
docs.oracle.com › javase › 8 › docs › api › java › math › BigDecimal.html
BigDecimal (Java Platform SE 8 )
October 20, 2025 - 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 these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
🌐
GitHub
gist.github.com › hyamamoto › 6762765
Generic null safe " Comparable.compareTo()" implementations. · GitHub
But at least I made it Java 6 (GWT/Android) compatible... Comparing.compareTo( Object, Object) Comparing.intCompareTo( Number, Number) Comparing.longCompareTo( Number, Number) Comparing.floatCompareTo( Number, Number) Comparing.doubleCompareTo( Number, Number) Comparing.bigDecimalCompareTo( Number, Number) Comparing.bigDecimalCompareTo( BigDecimal, BigDecimal) Comparing.stringCompareTo( Object, Object) Comparing.stringCompareTo( String, String) Apache Commons: ObjectUtils.compare(Object, Object) Guava: ComparisonChain.start() Java 7: Objects.compare(Object) Raw ·
🌐
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.
🌐
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 these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
Find elsewhere
🌐
Tabnine
tabnine.com › home page › code › java › java.math.bigdecimal
java.math.BigDecimal.compareTo java code examples | Tabnine
private void validateValue(BigDecimal value) { if (BigDecimal.ZERO.compareTo(value) > 0 || new BigDecimal(100).compareTo(value) < 0) { throw new IllegalArgumentException("Value must be between 0 and 100"); } } ... public static LimitOrder adaptOrder(CexIOFullOrder cexIOOrder) { OrderType orderType = cexIOOrder.type.equals("sell") ? OrderType.ASK : OrderType.BID; BigDecimal originalAmount = new BigDecimal(cexIOOrder.amount); CurrencyPair currencyPair = new CurrencyPair(cexIOOrder.symbol1, cexIOOrder.symbol2); Date timestamp = new Date(cexIOOrder.time); BigDecimal limitPrice = new BigDecimal(cexIOOrder.price); Order.OrderStatus status = adaptOrderStatus(cexIOOrder); BigDecimal cumulativeAmount = null; BigDecimal remains = new BigDecimal(cexIOOrder.remains); cumulativeAmount = originalAmount.subtract(remains); ?
🌐
BibSonomy
bibsonomy.org › url › e979ca536708bfd9395d2b237213a5f2
java - How to simplify a null-safe compareTo() implementation? - Stack Overflow | BibSonomy
bigdecimal · compareto · equals · java · null · nullable · nullsafe · safe · Please log in to take part in the discussion (add own reviews or comments).
🌐
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 − ·
🌐
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 ·
🌐
OpenJDK
bugs.openjdk.org › browse › JDK-6321015
BigDecimal should have comparison methods that return a ...
EXPECTED VERSUS ACTUAL BEHAVIOR : EXPECTED - Methods that return a boolean to indicate if the value is greater than, greater or equal than, less than or less or equal than the supplied parameter. ACTUAL - There is no real actual behavior, except the method compareTo(BigDecimal other), which returns an integer.
🌐
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 (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is: (x.compareTo(y) <op> 0), where <op> is one of the six comparison operators.
🌐
CopyProgramming
copyprogramming.com › howto › handling-null-for-bigdecimal
Java: Dealing with BigDecimal Null Values
June 14, 2023 - To steer clear of explicit null values, one might want to contemplate utilizing Optional<BigDecimal>. However, this ultimately boils down to personal preference. How to check if BigDecimal variable == 0 in java?, if (selectPrice.equals (BigDecimal.ZERO)) Note that .equals () takes scale into account, so unless selectPrice is the same scale (0) as .ZERO then this will return false. To take scale out of the equation as it were: if (selectPrice.compareTo (BigDecimal.ZERO) == 0) Usage exampleif (price.compareTo(BigDecimal.ZERO) == 0) // see belowFeedback ·
🌐
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 don't use too many digits. But the first one seems more reliable. ... JavaRanch-FAQ HowToAskQuestionsOnJavaRanch UseCodeTags DontWriteLongLines ItDoesntWorkIsUseLess FormatCode JavaIndenter SSCCE API-17 JLS JavaLanguageSpecification MainIsAPain KeyboardUtility