How to reset BigDecimal to zero

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

Thank @!

+5
source share
2 answers

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

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

You cannot change the value. BigDecimalsare immutable. You need to create a new one.

+8
source

All Articles