Search code examples
javabigdecimal

How to reset BigDecimal to zero


Hallo, I have a BigDecimal temp variable, I want it to be reusable in a function. Is there a way for me to reset this variable to zero if the value is greater than zero?

THanks @!


Solution

  • BigDecimal is immutable, and instances cannot be modified. However, you could do something like:

    public void myMethod(BigDecimal b) {
        BigDecimal zero = BigDecimal.ZERO;
        if (b.compareTo(zero) > 0)
            b = zero;
        // Do stuff with b here
    }