Java.math.BigDecimal.equals() Method



Description

The java.math.BigDecimal.equals() method compares this BigDecimal with the specified Object for equality. Unlike compareTo, this method considers two BigDecimal objects equal only if they are equal in value and scale (thus 2.0 is not equal to 2.00 when compared by this method).

Declaration

Following is the declaration for java.math.BigDecimal.equals() method.

public boolean equals(Object x)

Overrides

equals in class Object.

Parameters

x − Object to which this BigDecimal is to be compared.

Return Value

This method returns true if and only if the specified Object is a BigDecimal whose value and scale are equal to this BigDecimal's.

Exception

NA

Example

The following example shows the usage of math.BigDecimal.equals() method.

package com.tutorialspoint;

import java.math.*;

public class BigDecimalDemo {

   public static void main(String[] args) {

      // create 3 BigDecimal objects
      BigDecimal bg1, bg2, bg3;

      bg1 = new BigDecimal("235.00");
      bg2 = new BigDecimal("235.00");
      bg3 = new BigDecimal("235");

      // create 2 boolean objects
      Boolean b1,b2;

      // assign the result of equals method to b1, b2
      b1 = bg1.equals(bg2);
      b2 = bg1.equals(bg3);

      String str1 = bg1 + " equals " + bg2 + " is " +b1;
      String str2 = bg1 + " equals " + bg3 + " is " +b2;

      // print b1, b2 values
      System.out.println( str1 );
      System.out.println( str2 );
   }
}

Let us compile and run the above program, this will produce the following result −

235.00 equals 235.00 is true
235.00 equals 235 is false
java_math_bigdecimal.htm
Advertisements