To be short:

firstBigDecimal.compareTo(secondBigDecimal) < 0   // "<"
firstBigDecimal.compareTo(secondBigDecimal) > 0   // ">"    
firstBigDecimal.compareTo(secondBigDecimal) == 0  // "=="  
firstBigDecimal.compareTo(secondBigDecimal) != 0  // "!="  
firstBigDecimal.compareTo(secondBigDecimal) >= 0  // ">="    
firstBigDecimal.compareTo(secondBigDecimal) <= 0  // "<="    
Answer from torina on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › java › bigdecimal-compareto-function-in-java
BigDecimal compareTo() Function in Java - GeeksforGeeks
January 9, 2026 - The BigDecimal.compareTo() method compares two BigDecimal objects based on their numerical value only, ignoring scale differences, and returns an integer indicating whether the current value is less than, equal to, or greater than the specified ...
🌐
Coderanch
coderanch.com › t › 375912 › java › BigDecimal-compareTo
BigDecimal compareTo() problem (Java in General forum at Coderanch)
February 17, 2005 - 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.
🌐
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?
🌐
IncludeHelp
includehelp.com › java › bigdecimal-compareto-method-with-example.aspx
Java BigDecimal compareTo() Method with Example
May 3, 2020 - public int compareTo(BigDecimal ob); Parameter(s): BigDecimal ob – represents the object to be compared to this BigDecimal object. Return value: The return type of this method is int, it may return anyone of the given values, When (this BigDecimal) == (BigDecimal ob), it returns 0. When (this ...
🌐
Qlik Community
community.qlik.com › t5 › Talend-Studio › syntax-to-compare-BigDecimal-if-it-s-greater-than-x › td-p › 2267597
Solved: syntax to compare BigDecimal if it's greater than ... - Qlik Community - 2267597
October 1, 2019 - firstBigDecimal.compareTo(secondBigDecimal) < 0 // "<" firstBigDecimal.compareTo(secondBigDecimal) > 0 // ">" firstBigDecimal.compareTo(secondBigDecimal) == 0 // "==" firstBigDecimal.compareTo(secondBigDecimal) >= 0 // ">=" so use: BigDecimal.valueOf((row1.achievement.compareTo(BigDecimal.valueOf(0.7)>=0)?
🌐
HackerRank
hackerrank.com › challenges › java-bigdecimal › forum › comments › 121539
Java BigDecimal Discussions | Java | HackerRank
Arrays.sort(s, Collections.rev... use the BigDecimal values to //compare the strings! BigDecimal a = new BigDecimal(a1); BigDecimal b = new BigDecimal(a2); return a.compareTo(b); } })); } View more Comments.....
Find elsewhere
🌐
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 ...
🌐
Codemia
codemia.io › knowledge-hub › path › compare_if_bigdecimal_is_greater_than_zero
Compare if BigDecimal is greater than zero
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
W3Schools
w3schools.com › java › java_math.asp
Java Math
assert abstract boolean break byte case catch char class continue default do double else enum exports extends final finally float for if implements import instanceof int interface long module native new package private protected public return requires short static super switch synchronized this throw throws transient try var void volatile while Java String Methods · charAt() codePointAt() codePointBefore() codePointCount() compareTo() compareToIgnoreCase() concat() contains() contentEquals() copyValueOf() endsWith() equals() equalsIgnoreCase() format() getBytes() getChars() hashCode() indexOf() isEmpty() join() lastIndexOf() length() matches() offsetByCodePoints() regionMatches() replace() replaceAll() replaceFirst() split() startsWith() subSequence() substring() toCharArray() toLowerCase() toString() toUpperCase() trim() valueOf() Java Math Methods ·
🌐
Medium
bterczynski.medium.com › comparing-bigdecimals-for-equality-71037fa2ee1d
Comparing BigDecimals for Equality | by Brian Terczynski | Medium
January 23, 2022 - And so it might not look as natural ... equals() actually does. Anyways, to summarize: use compareTo() when you only want to check if the values of two BigDecimals are equal....
🌐
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 ); } }
🌐
Igor's Techno Club
igorstechnoclub.com › java-bigdecimal
The Pitfalls of Comparing BigDecimals in Java | Igor's Techno Club
June 17, 2024 - 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.
🌐
SAP Community
community.sap.com › t5 › technology-q-a › bigdecimal › qaq-p › 3010516
Solved: Bigdecimal
Official SAP Community. Search questions and answers, read the latest blog posts and curated content, connect with experts, and improve your SAP skills.
Top answer
1 of 4
15

The reason you have two different methods is that they do two different things.

The .equals method returns a boolean value indicating whether the object on which you call the method is equal to the object passed in as a parameter (for some definition of "is equal to" that is consistent with the nature of the object being compared).

The .compareTo method returns a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object. That makes it a useful method for sorting; it allows you to compare one instance with another for purposes of ordering.

When the Java documentation says that these two methods must behave consistently, what they mean is that the .equals method must return true in exactly the same situations where the .compareTo method returns zero, and must return false in exactly the same situations where the .compareTo method returns a nonzero number.

Is there any good reason to violate these rules? Generally no, for the same reasons that

#define TRUE FALSE

is a really bad idea. The only legitimate reason for inconsistent behavior is hinted at in the Java documentation itself: "[if the] class has a natural ordering that is inconsistent with equals."

To drive home the point, you can actually define .equals() in terms of compareTo(), thus guaranteeing consistent behavior. Consider this .equals() method from a Rational class which, after a few sanity checks, simply defines .equals as compareTo() == 0:

public boolean equals(Object y) {
    if (y == null) return false;
    if (y.getClass() != this.getClass()) return false;
    Rational b = (Rational) y;
    return compareTo(b) == 0;
}
2 of 4
8

This confusion would occur in situations where there are conflicting understandings of equals.

compareTo is 'easy' in that it asks a simple question 'which one is bigger?' If you were given a bunch of Things, how do you sort them?

equals on the other hand wants to ask 'are these the same thing?'

BigDecimal in Java is one such place where there are conflicting understandings of what it means to have two things being equal.

BigDecimal foo = new BigDecimal("1.00");
BigDecimal bar = new BigDecimal("1.000");

These two entires will have the same meaning for sorting. They, however, are not the same when it comes to equality. They have a different underlying state (the precision of the number) and that means they are not equal but foo.compareTo(bar) will return 0.

Consider this, if you had a Map qux = HashMap<BigDecimal, Object>() for some reason, do you want qux.put(foo,foo) to take the same spot as qux.put(bar,bar) and thus evict the earlier insertion?

So, while they are math equals (which is how compareTo sorts them), they are not inner state equals, and thus the necessity of the inconsistency here.

Yes, this inconsistency comes at the price of a higher cognitive load for dealing with BigDecimal. It means maps may not behave like you want them to... and the question is "which map do you want to behave 'right'?"

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.TreeMap;

class Main {
    public static void main (String[] args) {
        BigDecimal foo = new BigDecimal("1.00");
        BigDecimal bar = new BigDecimal("1.000");

        HashMap<BigDecimal, String> hash = new HashMap();
        TreeMap<BigDecimal, String> tree = new TreeMap();

        hash.put(foo, "foo");
        hash.put(bar, "bar");

        tree.put(foo, "foo");
        tree.put(bar, "bar");

        System.out.println("hash foo: " + hash.get(foo));
        System.out.println("hash bar: " + hash.get(bar));

        System.out.println("tree foo: " + tree.get(foo));
        System.out.println("tree bar: " + tree.get(bar));
    }
}

ideone

Output:

hash foo: foo
hash bar: bar
tree foo: bar
tree bar: bar

Because compareTo returned 0, in the TreeMap, bar evicted foo when bar was inserted. However, because these are different objects with different internal state and thus different hash codes, they were both able to exist within a HashMap.

From the docs:

Note: care should be exercised if BigDecimal objects are used as keys in a SortedMap or elements in a SortedSet since BigDecimal's natural ordering is inconsistent with equals. See Comparable, SortedMap or SortedSet for more information.

And so, thats the problem, the inconsistency and the "there's no good answer to this" dilemma.


One could also imagine this with a Rational class where one wants to keep 2/4 having the internal state of 2/4 so that:

Rational twoFourths = new Rational(2,4);
Rational oneHalf = new Rational(1,2);

System.out.println(twoFourths); // prints 2/4
System.out.println(oneHalf);    // prints 1/2
System.out.println(twoFourths.compareTo(oneHalf)); // prints 0
System.out.println(twoFourths.equals(oneHalf));    // ???

And you are once again up against that same question. Are these equal with the mathematical sense of equality (which means the hashCode needs to also return the same value?) or are they not equal using the object oriented state of the object sense of equality despite being the 'same'.

🌐
Baeldung
baeldung.com › home › java › core java › bigdecimal equals() vs. compareto()
BigDecimal equals() vs. compareTo() | Baeldung
May 2, 2024 - For BigDecimal.equals() to return ... points to the same behavior: Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale....
🌐
Java2Blog
java2blog.com › home › core java › bigdecimal › bigdecimal compareto
BigDecimal compareTo - Java2Blog
January 12, 2021 - Let’s see compareTo method with the help of example. Above program will generate below output. First BigDecimal is greater than Second BigDecimal
🌐
Codemia
codemia.io › knowledge-hub › path › bigdecimal_equals_versus_compareto
BigDecimal equals versus compareTo
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises
🌐
Mbukowicz
mbukowicz.github.io › java › 2015 › 11 › 14 › comparing-bigdecimals.html
Comparing BigDecimals
November 14, 2015 - BigDecimal a = new BigDecimal("2.00"); BigDecimal b = new BigDecimal("2.0"); System.out.println( a.compareTo(b) == 0 ); // true
🌐
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: (<, >, <=, >=).