One problem with BigDecimal today is that no matter how it evaluates, it does not change the value it ends up with, using the recommended API. Look at the code:
BigDecimal totalPay = new BigDecimal("0");
BigDecimal totalIncomeOfMaster = new BigDecimal("0");
BigDecimal totalIncomeOfLB = new BigDecimal("0");
BigDecimal totalRebate = new BigDecimal("0");
if (CollectionUtils.isNotEmpty(rebateDetailVOS.getList())) {
for(RebateDetailVO rebateDetailVO : rebateDetailVOS.getList()) { totalPay.add(rebateDetailVO.getFinalPayOfUser()); totalIncomeOfMaster.add(rebateDetailVO.getFinalIncomeOfMaster()); totalIncomeOfLB.add(rebateDetailVO.getFinalIncomeOfLB()); totalRebate.add(rebateDetailVO.getRebateAmt()); }}Copy the code
At first glance, the code was fine, but the result was not as expected, so the author went inside the add() method to look at the source code:
The problem is that a New BigDecimal object is returned, so the sum is not recorded. Then the author changes the code:
BigDecimal totalPay = new BigDecimal("0");
BigDecimal totalIncomeOfMaster = new BigDecimal("0");
BigDecimal totalIncomeOfLB = new BigDecimal("0");
BigDecimal totalRebate = new BigDecimal("0");
if (CollectionUtils.isNotEmpty(rebateDetailVOS.getList())) {
for(RebateDetailVO rebateDetailVO : rebateDetailVOS.getList()) { totalPay = totalPay.add(rebateDetailVO.getFinalPayOfUser()); totalIncomeOfMaster = totalIncomeOfMaster.add(rebateDetailVO.getFinalIncomeOfMaster()); totalIncomeOfLB = totalIncomeOfLB.add(rebateDetailVO.getFinalIncomeOfLB()); totalRebate = totalRebate.add(rebateDetailVO.getRebateAmt()); }}Copy the code
Results meet expectations