Daniel Crook Daniel Crook - 11 months ago 63
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 Source

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)
        //this evaluates to true