adding 2 BigDecimal values [duplicate]

て烟熏妆下的殇ゞ 提交于 2019-12-03 04:20:36

BigDecimal is immutable. Every operation returns a new instance containing the result of the operation:

 BigDecimal sum = x.add(y);

If you want x to change, you thus have to do

x = x.add(y);

Reading the javadoc really helps understanding how a class and its methods work.

Perhaps this is what you prefer:

BigDecimal z = new BigDecimal(5).add(x);

Every operation of BigDecimal returns a new BigDecimal but not change the current instance.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!