Table of Contents
In this post,we will see about Bigdecimal‘s compareTo method.This method is used to compare two BigDecimals.
Syntax
1 2 3 |
public int CompareTo(BigDecimal b) |
Return type
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 BigDecimal
BigDecimal CompareTo example
Let’s see compareTo method with the help of example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
import java.math.*; class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bigdecimal1 = new BigDecimal("80"); BigDecimal bigdecimal2 = new BigDecimal("40"); //comparing bigdecimal1 and bigdecimal2 int result = bigdecimal1.compareTo(bigdecimal2); if( result == -1 ) System.out.println("First BigDecimal is less than Second BigDecimal"); else if( result == 0) System.out.println("First BigDecimal is equal to Second BigDecimal"); else if( result == 1 ) System.out.println("First BigDecimal is greater than Second BigDecimal"); } } |
Above program will generate below output.
BigDecimal compareTo vs equals
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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
import java.math.*; class BigDecimalDemo { public static void main(String[] args) { // create 2 BigDecimal objects BigDecimal bigdecimal1 = new BigDecimal("20.0"); BigDecimal bigdecimal2 = new BigDecimal("20.000"); System.out.println("Using BigDecimal compareTo method"); //comparing bigdecimal1 and bigdecimal2 using CompareTo method int result = bigdecimal1.compareTo(bigdecimal2); if( result == -1 ) System.out.println("First BigDecimal is less than Second BigDecimal"); else if( result == 0) System.out.println("First BigDecimal is equal to Second BigDecimal"); else if( result == 1 ) System.out.println("First BigDecimal is greater than Second BigDecimal"); System.out.println("================="); System.out.println("Using BigDecimal equals method"); //comparing bigdecimal1 and bigdecimal2 using equals method boolean equalsResult=bigdecimal1.equals(bigdecimal2); if(equalsResult) System.out.println("First BigDecimal is equal to Second BigDecimal"); else System.out.println("First BigDecimal is not equal to Second BigDecimal"); } } |
When you run above program, you will get below output.
First BigDecimal is equal to Second BigDecimal
=================
Using BigDecimal equals method
First BigDecimal is not equal to Second BigDecimal
Comparing BigDecimal with Zero
There are multiple ways to compare a BigDecimal with 0.Best way is:
1 2 3 |
bigDecimal.compareTo(BigDecimal.ZERO)>0 |
Let’s see with the help of simple example.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
import java.math.*; class BigDecimalDemo { public static void main(String[] args) { // create BigDecimal object BigDecimal bigdecimal1 = new BigDecimal("1.0"); //comparing bigdecimal1 and zero using CompareTo method int result = bigdecimal1.compareTo(BigDecimal.ZERO); if( result == -1 ) System.out.println("BigDecimal is less than Zero"); else if( result == 0) System.out.println("BigDecimal is equal to Zero"); else if( result == 1 ) System.out.println("BigDecimal is greater than Zero"); } } |
When you run above program, you will get below output.
That’s all about BigDecimal CompareTo method.