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 @!
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.
If you are using type BigDecimal, then its default value is null (it is object, not primitive type), so you get [1] automatically.
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.
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
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With