Check if BigDecimal is integer value

前端 未结 7 2144
有刺的猬
有刺的猬 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:42

    This can be done the same way that someone would check if a double is an integer, performing % 1 == 0. This is how it would look for a BigDecimal value.

    public static boolean isIntegerValue(BigDecimal bd){
      return bd.remainder(BigDecimal.ONE).compareTo(BigDecimal.ZERO) == 0;
    }
    

提交回复
热议问题