Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal is not doing add operation [duplicate]

I have 2 BigDecimal numbers. I am trying to add them. My code is as follow:

BigDecimal bd1 = new BigDecimal(10);
BigDecimal bd2 = new BigDecimal(10);

bd1.add(bd2);

Here I am expecting the value of bd1 20 but again and again it's showing 10. It's not being added. Please help if I have done something wrong.

like image 571
zaz Avatar asked Dec 01 '22 21:12

zaz


1 Answers

BigDecimal values are immutable, You need to assign the value to the result of add:

bd1 = bd1.add(bd2);
like image 169
Reimeus Avatar answered Dec 09 '22 14:12

Reimeus