'BigDecimal += (add and assign) ...How to do this?

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



Solution 1:[1]

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.

Solution 2:[2]

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)

Solution 3:[3]

BigDecimals are objects. the += operator only works with primitives.

So I don't think you can do what you are trying to propose.

Solution 4:[4]

You can use the BigDecimal#add() method .

a = a.add(b);

Since BigDecimal is immutable , you cannot expect a.add(b) to mutate a . You have to return the new BigDecimal object to the reference a . Hence a=a.add(b) is what you need.

Solution 5:[5]

Nope - there is no equivalent mechanism

You just have to use the add method

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 JHS
Solution 2
Solution 3 jh314
Solution 4
Solution 5 DaveH