Check if BigDecimal is integer value

前端 未结 7 2147
有刺的猬
有刺的猬 2021-02-03 17:36

Can anyone recommend an efficient way of determining whether a BigDecimal is an integer value in the mathematical sense?

At present I have

7条回答
  •  一生所求
    2021-02-03 17:44

    Divide the number by 1 and check for a remainder. Any whole number should always have a remainder of 0 when divided by 1.

    public boolean isWholeNumber(BigDecimal number) {
        return number.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0;
    }
    

提交回复
热议问题