BigDecimal正確的累加計數姿勢

還在爲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())));
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章