How to Check whether Bigdecimal value is zero or not in Java

Sometimes, , it becomes necessary to check whether a BigDecimal value is zero or not. A BigDecimal is considered zero if it can store values like 0, 0.0, or 0.00, among others. To check whether a BigDecimal object is zero, the enum constant BigDecimal.ZERO representing 0 with zero scales can be utilized.

How do you check whether BigDecimal is zero or not in java

There are multiple ways to check whether BigDecimal is zero or not in java.

  • equals
  • compareTo
  • signum

Let’s see the difference between those three methods

  • equals method

    The equals method compares a BigDecimal with BigDecimal.ZERO and returns a boolean value. It does not consider the scale for equality testing. Here’s an example:

          public boolean equals(Object x)
    • “0”.equals(BigDecimal.ZERO) returns true and there is no scale in the given object
    • “0.0”.equals(BigDecimal.ZERO) returns false and equals method does not check
    • “0.00”.equals(BigDecimal.ZERO) returns false and equals method does not use scale for equality.

    Here is a BigDecimal equals check for zero or not.

    import java.math.BigDecimal;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
            BigDecimal bd = new BigDecimal(0);
            System.out.println(new BigDecimal("0").equals(BigDecimal.ZERO));// true);
            System.out.println(new BigDecimal("0.0").equals(BigDecimal.ZERO));// false
            System.out.println(new BigDecimal("0.00").equals(BigDecimal.ZERO));// false
    
        }
    }
  • compareTo method

    The compareTo method checks both the zero value and zero scale value, returning 0 if both BigDecimal objects are zero.

    It is null-safe and more readable than the equals method. Syntax:

        public int compareTo(BigDecimal val)

    This method returns 0, if both BigDecimal objects are zero, else return integer other zero.

    It returns 0 for all the below cases.

    • “0”.compareTo(BigDecimal.ZERO)
    • “0.0”.compareTo(BigDecimal.ZERO)
    • “0.00”.compareTo(BigDecimal.ZERO)

    Here’s an example:

    import java.math.BigDecimal;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
            BigDecimal bd = new BigDecimal(0);
    
    
            System.out.println(new BigDecimal("0").compareTo(BigDecimal.ZERO) == 0);// true;
            System.out.println(new BigDecimal("0.0").compareTo(BigDecimal.ZERO) == 0);// true
            System.out.println(new BigDecimal("0.00").compareTo(BigDecimal.ZERO) == 0);// true
    
        }
    }
  • Signum method

    The signum method returns the signum of a BigDecimal object, indicating -1, 0, or 1. While it is not null-safe and throws a NullPointerException, it can be used for checking if the value is zero.

        public int signum()

    It returns possible values -1,0,1. Example:

    import java.math.BigDecimal;
    
    public class BigDecimalTest {
        public static void main(String[] args) {
            BigDecimal bd = new BigDecimal(0);
            System.out.println(new BigDecimal("0").signum() == 0);// true;
            System.out.println(new BigDecimal("0.0").signum() == 0);// true;
            System.out.println(new BigDecimal("0.00").signum() == 0);// true;
    
        }
    }

Conclusion

In conclusion, there are multiple ways to check if a given BigDecimal object is zero. The method is less considerate of scale, the method is null-safe and more readable, while the signum method is not null-safe and less readable. Choose the method that best suits your need.