Java Data Type Tutorial - Java BigDecimal .compareTo (BigDecimal val)








Syntax

BigDecimal.compareTo(BigDecimal val) has the following syntax.

public int compareTo(BigDecimal val)

Example

In the following code shows how to use BigDecimal.compareTo(BigDecimal val) method.

import java.math.BigDecimal;
/*  w w w  .  j  av a 2s. c o m*/
public class Main {

  public static void main(String[] args) {

    BigDecimal bg1 = new BigDecimal("10");
    BigDecimal bg2 = new BigDecimal("20");

    int 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);
  }
}

The code above generates the following result.