還在爲BigDecimal累加計數爲"0"而苦惱麼?
(1) 錯誤姿勢, 這樣會導致amount一直爲 “0”
BigDecimal amount = BigDecimal.ZERO;
while (iterator.hasNext()) {
amount.add(mallProduct.getPrice().multiply(BigDecimal.valueOf(cartFromRedis.getQuantity())));
}
(2) 正確姿勢, 一定要在add()前添加上amount = amount.add()
BigDecimal amount = BigDecimal.ZERO;
while (iterator.hasNext()) {
amount = amount.add(mallProduct.getPrice().multiply(BigDecimal.valueOf(cartFromRedis.getQuantity())));
}