user3571618
user3571618

Reputation: 213

How to do BigDecimal modulus comparison

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!

Upvotes: 20

Views: 22373

Answers (2)

Alexey Malev
Alexey Malev

Reputation: 6533

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");
}

Upvotes: 18

Dawood ibn Kareem
Dawood ibn Kareem

Reputation: 79818

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

Upvotes: 29

Related Questions