compareTo(BigDecimal val) compares the BigDecimal Object with the specified BigDecimal value. Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method.
Java provides the built-in function compareTo() which compares the two BigDecimals . The comparison can not be done using the > , < or = operators as these operators can only be used for the primitive data types like int, long and double.
Use the compareTo method of BigDecimal : public int compareTo(BigDecimal val) Compares this BigDecimal with the specified BigDecimal. Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
5. Using the signum Method. The BigDeicmal class provides the signum method to tell if the given BigDecimal object's value is negative (-1), zero (0), or positive (1).
To be short:
firstBigDecimal.compareTo(secondBigDecimal) < 0 // "<"
firstBigDecimal.compareTo(secondBigDecimal) > 0 // ">"
firstBigDecimal.compareTo(secondBigDecimal) == 0 // "=="
firstBigDecimal.compareTo(secondBigDecimal) >= 0 // ">="
Every object of the Class BigDecimal
has a method compareTo
you can use to compare it to another BigDecimal. The result of compareTo
is then compared > 0
, == 0
or < 0
depending on what you need. Read the documentation and you will find out.
The operators ==
, <
, >
and so on can only be used on primitive data types like int
, long
, double
or their wrapper classes like Integer
and Double
.
From the documentation of compareTo
:
Compares this
BigDecimal
with the specifiedBigDecimal
.Two
BigDecimal
objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. This method is provided in preference to individual methods for each of the six boolean comparison operators (<, ==, >, >=, !=, <=). The suggested idiom for performing these comparisons is:(x.compareTo(y) <op> 0)
, where<op>
is one of the six comparison operators.Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
Use the compareTo
method of BigDecimal :
public int compareTo(BigDecimal val) Compares this BigDecimal with the specified BigDecimal.
Returns: -1, 0, or 1 as this BigDecimal is numerically less than, equal to, or greater than val.
Here is an example for all six boolean comparison operators (<, ==, >, >=, !=, <=):
BigDecimal big10 = new BigDecimal(10);
BigDecimal big20 = new BigDecimal(20);
System.out.println(big10.compareTo(big20) < -1); // false
System.out.println(big10.compareTo(big20) <= -1); // true
System.out.println(big10.compareTo(big20) == -1); // true
System.out.println(big10.compareTo(big20) >= -1); // true
System.out.println(big10.compareTo(big20) > -1); // false
System.out.println(big10.compareTo(big20) != -1); // false
System.out.println(big10.compareTo(big20) < 0); // true
System.out.println(big10.compareTo(big20) <= 0); // true
System.out.println(big10.compareTo(big20) == 0); // false
System.out.println(big10.compareTo(big20) >= 0); // false
System.out.println(big10.compareTo(big20) > 0); // false
System.out.println(big10.compareTo(big20) != 0); // true
System.out.println(big10.compareTo(big20) < 1); // true
System.out.println(big10.compareTo(big20) <= 1); // true
System.out.println(big10.compareTo(big20) == 1); // false
System.out.println(big10.compareTo(big20) >= 1); // false
System.out.println(big10.compareTo(big20) > 1); // false
System.out.println(big10.compareTo(big20) != 1); // true
You can use method named compareTo
, x.compareTo(y)
. It will return 0 if x and y are equal, 1 if x is greater than y and -1 if x is smaller than y
BigDecimal
isn't a primitive, so you cannot use the <
, >
operators. However, since it's a Comparable
, you can use the compareTo(BigDecimal)
to the same effect. E.g.:
public class Domain {
private BigDecimal unitPrice;
public boolean isCheaperThan(BigDecimal other) {
return unitPirce.compareTo(other.unitPrice) < 0;
}
// etc...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With