{

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

Sometimes, We want to do check bigdecimal value is zero or not

BigDecimal is zero means that it can store 0, 0.0 or 0.00 etc values.

So you want to check Integer is zero and the precision is also zero.

BigDecimal object has enum constant BigDecimal.ZERO to represent 0 value with zero scales.

use BigDecimal.ZERO to check object is zero or not.

## 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

It checks the Bigdecimal with BigDecimal.ZERO and returns a boolean value as given below. It does not use a scale for equality test. Syntax:

``````      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.

```java
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 zero value and zero scale value and returns 0. It is null-safe and more readable. You can check to give return 0 with double equal 0. 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 is an example code

``````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

It returns the signum of a BigDecimal object. It is not null safe and throws NullPointerException and not readable.

``````    public int signum()
``````

It returns possible values -1,0,1.

``````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

Multiple ways we can check given object is zero or not.

• equals is not considered the scale zero
• compareTo is null safe, readable, and considered scale zero
• signum is not readable, not null safe, and considered scale zero
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.

Related posts