In Java, BigDecimal is a class used for accurate calculations with decimal numbers, giving precise results for complex maths. Unlike primitive data types like int, float, and double, BigDecimal is used to perform calculations on large decimal values while avoiding errors that occur during floating-point arithmetic.
In this blog, we will learn how to check if a BigDecimal value is greater than zero.
Table of Contents:
Comparing if BigDecimal is greater than zero using the compareTo() function in Java
In Java, To compare whether a BigDecimal value is greater than zero or not, we have to use the compareTo() function. In primitive data types we can do this comparison with the help of relational operators like: (<, >, <=, >=). But for BigDecimal values, we have to use the compareTo() function.
Syntax:
public int compareTo(BigDecimal val)
Parameters: It takes only one BigDecimal Object as a parameter.
Return Value:
- It returns 1 if the value is greater than the parameter value.
- It returns 0 if the value is equal to the parameter value.
- It returns -1 if the value is smaller than the parameter value.
Practical Examples of Comparing BigDecimal to Zero in Java
Let’s learn the concept of compareTo() method in Java with the help of some examples:
Case 1: For greater than condition
Let’s learn an example of greater than condition:
Code:
import java.math.BigDecimal;
public class BigDecimalComparisonExamples {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("20.52");
BigDecimal num2 = new BigDecimal("20.367896789");
if (num1.compareTo(num2) > 0) {
System.out.println(num1 + " is greater than " + num2);
}
else{
System.out.println(num1 + " is lesser than " + num2);
}
}
}
Output:
20.52 is greater than 20.367896789
Explanation: In the above code, there are 2 decimal values num1 and num2, by comparing their values with the help of compareTo() function, the function returns 1, it means num1 is greater than num2.
Get 100% Hike!
Master Most in Demand Skills Now!
Case 2: For equal condition
Let’s learn an example of equal to condition:
Code:
import java.math.BigDecimal;
public class BigDecimalComparisonExamples {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("20.5");
BigDecimal num2 = new BigDecimal("20.500");
if (num1.compareTo(num2) == 0) {
System.out.println(num1 + " is equal to " + num2);
}
else{
System.out.println(num1 + " is not equal to " + num2);
}
}
}
Output:
20.5 is equal to 20.500
Case 3: For less than condition
Let’s learn an example of less than condition:
Code:
import java.math.BigDecimal;
public class BigDecimalComparisonExamples {
public static void main(String[] args) {
BigDecimal num1 = new BigDecimal("20.5456");
BigDecimal num2 = new BigDecimal("20.6");
if (num1.compareTo(num2) > 0) {
System.out.println(num1 + " is greater than " + num2);
}
else{
System.out.println(num1 + " is lesser than " + num2);
}
}
}
Output:
20.5456 is less than 20.6
Conclusion
So far in this article, we have learned BigDecimal and its comparison in Java. We have learned how to compare if a BigDecimal value is greater than zero or not. In primitive data types, we can do this comparison with the help of relational operators, while in BigDecimal, we have to use the CompreTo() function.
If you want to excel in your career in Java, you may refer to our Java Course.
FAQs
1. Why use BigDecimal instead of double?
BigDecimal is used instead of double for more precise calculations.
2. Is BigDecimal a float?
No, BigDecimal is not a float, it is a class that is used for precise calculations of decimal values. BigDecimal gives more accurate results than float and double.
3. What is BigDecimal in Java?
BigDecimal in Java is a class used to handle decimal numbers with high precision.
4. Is BigDecimal a primitive?
BigDecimal is not a primitive data type.