Daniel Crook Daniel Crook - 1 month ago 7
Java Question

For loop comparing Java BigDecimals

I need the loop to repeat as long as

BigDecimal d
is greater than 0. I have tried two following methods and neither of these methods seem to be working. Thanks in advance for any advice.

for (d.compareTo(z) < 0 ; ) {

}

for (BigDecimal d>0) {

}

Answer

To perform the greater than operation you have to use compareTo() method.

 for(d.compareTo(new BigDecimal("0"))>0; ){  //Your code }

Example :

        BigDecimal d = new BigDecimal("10");
        if(d.compareTo(new BigDecimal("0"))>0)
            System.out.println("true");
        //this evaluates to true