This article will elaborate on the approaches to compare two “BigDecimals” in Java.
What is “BigDecimal” in Java?
A “BigDecimal” in Java comprises a 32-bit integer scale and is utilized to handle very large and very small floating-point numbers. The “scale” represents the number of digits to the right of the decimal point.
How to Compare Two BigDecimals in Java?
To compare two BigDecimals in Java, apply the following approaches:
Before heading to the approaches, make sure to include the below-provided package to access the “math” class methods:
Approach 1: Compare Two BigDecimals in Java Using the “compareTo()” Method
The “compareTo()” method compares the two “BigDecimals” and returns the outcome based on the following parameters:
Returned Outcome | Computed Comparison |
---|---|
1 | If the former BigDecimal is greater than the latter BigDecimal. |
0 | In the case of the former BigDecimal being equal to the latter BigDecimal. |
-1 | When the former BigDecimal is less than the latter BigDecimal. |
This method can be applied to compare the two created “BigDecimal” values and return the corresponding outcome via the “if/else” statement.
Syntax
According to this syntax, “Object obj” corresponds to the object that needs to be compared.
Example
The below-provided example explains the discussed concept:
public static void main(String args[]) {
BigDecimal val1, val2;
val1 = new BigDecimal("26326.04");
val2 = new BigDecimal("22145.20");
if (val1.compareTo(val2) == 0) {
System.out.println(val1 + " and " + val2 + " are equal");
}
else if (val1.compareTo(val2) == -1) {
System.out.println(val1 + " is less than " + val2);
}
else {
System.out.println(val1.compareTo(val2));
System.out.println(val1 + " is greater than " + val2);
}
According to the above lines of code, apply the following steps:
- First of all, create two “BigDecimals” having the stated values.
- Now, associate the “compareTo()” method with both the assigned values, and upon the satisfied condition in the “if” statement, log the corresponding message.
- Note: The conditions are specified based on the method’s returned outcome to make the method work properly.
- Likewise, the “else if” statement specifies the other condition considering the method return type, i.e., “-1”.
- Finally, make the “else” statement come into effect upon both the above-unsatisfied conditions.
Output
In the above outcome, the returned “1” indicates that the “else” statement is invoked.
Approach 2: Compare Two BigDecimals in Java Using the “equals()” Method
The “equals()” method of the Java “BigDecimal” class is utilized to compare the BigDecimals for equality based on value and scale. This method can be implemented to apply a check upon the BigDecimals having a variation in their scale.
Syntax
In this syntax, “Object x” corresponds to the object with which this BigDecimal needs to be compared.
Example
Let’s overview the following example:
public static void main(String args[]) {
BigDecimal val1, val2;
val1 = new BigDecimal("205.0");
val2 = new BigDecimal("205.00");
if (val1.equals(val2)) {
System.out.println(val1 + " and " + val2 + " are equal");
}
else {
System.out.println(val1 + " and " + val2 + " are not equal");
}
}}
According to the above code:
- Likewise, create two BigDecimals having variation in their scale.
- Note: “0” is not equal to “2.00” when compared to this method.
- After that, apply the “equals()” method to compare the created BigDecimals and return the corresponding outcome via the “if/else” statement.
Output
In this outcome, it can be observed that both the “BigDecimals” are not equal due to variation in scale.
However, the following outcome leads to making the “BigDecimals” equal:
That’s how you can compare two “BigDecimals” in Java.
Conclusion
A “BigDecimal” in Java comprises a 32-bit integer scale. The two BigDecimals in Java can be compared by applying the “compareTo()”, or the “equals()” methods. The former approach returns the output based on the calculated comparison. The latter approach analyzes the BigDecimal values based on the specified scales. This blog is guided to compare the two “BigDecimals” in Java.