BigDecimal class compareTo() method analyzes this.object value with the specified object value numerically.
Java BigDecimal compareTo example
BigDecimal class compareTo() method analyzes this.object value with the specified object value numerically. This means method numerically compares the value of the object on which it is invoked, with the value of the mentioned object. During this method process, the object values are compared to each other numerically. Method return type is int therefore it returns only integer values.
It returns negative value -1, number zero 0 & positive value +1. Now, negative value indicates that specified object is numerically greater than object on which the method is invoked. Positive value signifies that the mentioned object is numerically lesser than object on which the method is invoked. And finally number zero means both object values are numerically equal.
More simply -1 indicates that this.object is lesser than the specified object numerically. 0 (zero) means this. object value is numerically equal with the specified object value, & +1 means this.object value is numerically greater than the specified object value.
Syntax for using the method:
System.out.println(bigdecimal_objectName.compareTo(BigDecimal x));
or
int x = this.object.compareTo(x);
Having numerical equal values does not states that the scales defined for the objects value will also match with each other. The scale difference is not undertaken into account for observation by the method so, no error is generated regarding scales topic. Method throws NumberFormatException if it finds a value other than a integer or double value.
Java_BigDecimal_compareTo.java
import java.math.MathContext;
|