문제

Im getting confused on how to write a simple modulus comparison if statement. I basically just want to check if x is a multiple of 20 when x is a BigDecimal. Thanks!

도움이 되었습니까?

해결책

if( x.remainder(new BigDecimal(20)).compareTo(BigDecimal.ZERO) == 0 ) {
   // x is a multiple of 20
}

다른 팁

You should use remainder() method:

BigDecimal x = new BigDecimal(100);
BigDecimal remainder = x.remainder(new BigDecimal(20));
if (BigDecimal.ZERO.compareTo(remainder) == 0) {
    System.out.println("x can be divided by 20");
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top