I can't seem to locate an 'add and assign' method in the BigDecimal class.
Is there a method for this?
In case you don't understand my question I'm trying to do this:
a += b;
but I'm trying to do it with BigDecimals
add(BigDecimal val) is used to calculate the Arithmetic sum of two BigDecimals. This method is used to find arithmetic addition of large numbers of range much greater than the range of largest data type double of Java without compromising with the precision of the result.
You either never allow null values in database, application or view and initialize everything with new BigDecimal(0) or perform null checks on every usage for nullable values.
public enum RoundingMode extends Enum<RoundingMode> Specifies a rounding behavior for numerical operations capable of discarding precision. Each rounding mode indicates how the least significant returned digit of a rounded result is to be calculated.
I guess you would like something like this:
BigDecimal bd = getNumber();
bd.addAndAssign(5);
BigDecimal
is an immutable object, so no, you cannot do that.
You must use add()
and equal it to itself, e.g. a = a.add(b)
There is an add
method in the BigDecimal
class.
You would have to do - a = a.add(b);
Have a look at the java docs.
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