Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 710
Terence Chow Avatar asked Jun 22 '13 17:06

Terence Chow


People also ask

How do I add two BigDecimal values?

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.

Can we assign null to BigDecimal in Java?

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.

What is RoundingMode in BigDecimal Java?

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.


2 Answers

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)

like image 70
darijan Avatar answered Oct 18 '22 12:10

darijan


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.

like image 41
JHS Avatar answered Oct 18 '22 11:10

JHS