How to check if BigDecimal variable == 0 in java?

后端 未结 11 610
灰色年华
灰色年华 2020-12-04 06:29

I have the following code in Java;

BigDecimal price; // assigned elsewhere

if (price.compareTo(new BigDecimal(\"0.00\")) == 0) {
    return true;
}
<         


        
11条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-04 06:57

    There is a static constant that represents 0:

    BigDecimal.ZERO.equals(selectPrice)
    

    You should do this instead of:

    selectPrice.equals(BigDecimal.ZERO)
    

    in order to avoid the case where selectPrice is null.

提交回复
热议问题