The java.math.BigDecimal.compareTo(BigDecimal bg) method checks for equality of this BigDecimal and BigDecimal object bg passed as parameter. The method considers two equal BigDecimal objects even if they are equal in value irrespective of the scale.
Syntax:
public int compareTo(BigDecimal bg)
Parameters:This function accepts only one BigDecimal object of BigDecimal type for comparison with this BigDecimal object.
Return Value:This method can return the following values :
- 0 : if value of this BigDecimal is equal to that of BigDecimal object passed as parameter.
- 1 : if value of this BigDecimal is greater than that of BigDecimal object passed as parameter.
- -1 : if value of this BigDecimal is less than that of BigDecimal object passed as parameter.
Note: The function returns true when it compares 124.0 and 124.0000, as it does not compare the scale of the two BigDecimal objects.
Examples:
Input : b1 = new BigDecimal("4743.0008"), b2 = new BigDecimal("4743.00001")
b1.compareTo(b2)
Output : 1
Input : b1 = new BigDecimal(4743), b2 = new BigDecimal("4743.00");
b1.compareTo(b2)
Output : 0
Below programs illustrate the working of compareTo() function of BigDecimal class:
Program 1: For greater than condition:
// Java program to demonstrate compareTo() method import java.io.*; import java.math.*; public class GFG { public static void main(String[] args) { // Creating 2 BigDecimal objects BigDecimal b1, b2; b1 = new BigDecimal(67891); b2 = new BigDecimal(12346); if (b1.compareTo(b2) == 0) { System.out.println(b1 + " and " + b2 + " are equal."); } else if (b1.compareTo(b2) == 1) { System.out.println(b1 + " is greater than " + b2 + "."); } else { System.out.println(b1 + " is lesser than " + b2 + "."); } } } |
67891 is greater than 12346.
Program 2: For equal condition:
// Java program to demonstrate compareTo() method import java.io.*; import java.math.*; public class GFG { public static void main(String[] args) { // Creating 2 BigDecimal objects BigDecimal b1, b2; b1 = new BigDecimal(67891); b2 = new BigDecimal("67891.000"); if (b1.compareTo(b2) == 0) { System.out.println(b1 + " and " + b2 + " are equal."); } else if (b1.compareTo(b2) == 1) { System.out.println(b1 + " is greater than " + b2 + "."); } else { System.out.println(b1 + " is lesser than " + b2 + "."); } } } |
67891 and 67891.000 are equal.
Program 3: For less than condition:
// Java program to demonstrate compareTo() method import java.io.*; import java.math.*; public class GFG { public static void main(String[] args) { // Creating 2 BigDecimal objects BigDecimal b1, b2; b1 = new BigDecimal("4743.00001"); b2 = new BigDecimal("4743.0008"); if (b1.compareTo(b2) == 0) { System.out.println(b1 + " and " + b2 + " are equal."); } else if (b1.compareTo(b2) == 1) { System.out.println(b1 + " is greater than " + b2 + "."); } else { System.out.println(b1 + " is lesser than " + b2 + "."); } } } |
4743.00001 is lesser than 4743.0008.
Reference: https://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html#compareTo(java.math.BigDecimal)
Attention reader! Don’t stop learning now. Get hold of all the important Java and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready.
Recommended Posts:
- Java.lang.String.compareTo()
- Java Integer compareTo() method
- Java long compareTo() with examples
- Double.compareTo() Method in Java with Examples
- ByteBuffer compareTo() method in Java With Examples
- BigInteger compareTo() Method in Java
- FloatBuffer compareTo() method in Java With Examples
- CharBuffer compareTo() method in Java
- ShortBuffer compareTo method in Java with Examples
- DoubleBuffer compareTo() method in Java With Examples
- IntBuffer compareTo() method in Java
- Boolean compareTo() method in Java with examples
- Byte compareTo() method in Java with examples
- Short compareTo() method in Java with Examples
- Java | ==, equals(), compareTo(), equalsIgnoreCase() and compare()
- Instant compareTo() method in Java with Examples
- Duration compareTo(Duration) method in Java with Examples
- Year compareTo() method in Java with Examples
- LocalDate compareTo() method in Java
- YearMonth compareTo() method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

