Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 @!

like image 883
huahsin68 Avatar asked Feb 26 '11 04:02

huahsin68


People also ask

How do I know if BigDecimal is equal to zero?

Using the compareTo Method Two BigDecimal objects that are equal in value but have a different scale (like 2.0 and 2.00) are considered equal by this method. Therefore, we can check BigDecimal. ZERO. compareTo(givenBdNumber) == 0 to decide if givenBdNumber has the value zero.

What is the default value of BigDecimal?

If you are using type BigDecimal, then its default value is null (it is object, not primitive type), so you get [1] automatically.

How do I set big decimal value?

math. BigDecimal. valueOf(double val) is an inbuilt method in java that translates a double into a BigDecimal, using the double's canonical string representation provided by the Double. toString(double) method.


1 Answers

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
}
like image 102
Peter C Avatar answered Oct 07 '22 09:10

Peter C