BigDecimal equals() Method in Java
The java.math.BigDecimal.equals() method checks for equality of a BigDecimal value with the object passed. This method considers two BigDecimal objects equal if only if they are equal in value and scale.
Syntax:
public boolean equals(Object obj)
Parameters: This function accepts an Object obj as a mandatory parameter for comparison with this BigDecimal object.
Return Value: This method returns boolean true if and only if the Object passed as parameter is a BigDecimal whose value and scale are equal to that of this BigDecimal object and returns false otherwise. Thus, this function does not return true when it compares 124.0 and 124.0000.
Examples:
Input:
b1 = new BigDecimal("4743.00")
b2 = new BigDecimal("4743.00000")
Output: false
Input:
b1 = new BigDecimal(4743)
b2 = new BigDecimal("4743")
Output: true
Below programs illustrate equals() method of BigDecimal class:
Program 1:
// Java program to demonstrate equals() 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.00"); b2 = new BigDecimal("4743.00000"); if (b1.equals(b2)) { System.out.println(b1 + " and " + b2 + " are equal."); } else { System.out.println(b1 + " and " + b2 + " are not equal."); } } } |
4743.00 and 4743.00000 are not equal.
Program 2:
// Java program to demonstrate equals() 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"); if (b1.equals(b2)) { System.out.println(b1 + " and " + b2 + " are equal."); } else { System.out.println(b1 + " and " + b2 + " are not equal."); } } } |
67891 and 67891 are equal.
Recommended Posts:
- BigDecimal abs() Method in Java
- BigDecimal plus() method in Java
- BigDecimal max() Method in Java
- BigDecimal min() Method in Java
- BigDecimal ulp() Method in Java
- BigDecimal intvalueExact() Method in Java
- BigDecimal scaleByPowerOfTen() method in Java
- BigDecimal scale() Method in Java
- BigDecimal round() Method in Java
- BigDecimal longValueExact() Method in Java
- BigDecimal valueOf() Method in Java
- BigDecimal intValue() Method in Java
- BigDecimal pow() method in Java with Examples
- BigDecimal hashCode() Method in Java
- BigDecimal precision() 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.

