Question

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!

Était-ce utile?

La solution

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

Autres conseils

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");
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top