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.
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.
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:
4743.00 and 4743.00000 are not equal.
67891 and 67891 are equal.
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.