質問

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