Medardas
Medardas

Reputation: 540

Comparing two numbers

I have this program piece:

double stockWeight = 1;
    if(data[0] > 9 )        
        stockWeight = 1000*R(data[0]/10);
    double compare = data[0]*100-stockWeight;
    System.out.println(compare);
    if(compare > 300.0 && compare <=600.0)
        stockWeight += 300;
    else if(compare > 600.0 && compare <= 900.0)
        stockWeight += 600;
    else if(compare > 900.0);
        stockWeight += 900;

        System.out.println(stockWeight);

 ///////////////////////////////////
   private int R(double D){
      int howBIG = 1;
      if (D >= 100){howBIG = 10;}
      else if(D >= 1000){howBIG = 100;}
      DecimalFormat F = new DecimalFormat("#");
      return Integer.valueOf(F.format(D/howBIG))*howBIG;
  }

the output is:

126.0
1900.0

All numbers here are double type. data[0] = 11.26 why does my computer think that 126.0 is greater than 900.0?? R method basically is div function of 10(100) if greater than 100(1000), gives whole number

Upvotes: 0

Views: 655

Answers (1)

slothrop
slothrop

Reputation: 5418

You are likely seeing the confusing result because of the stray semicolon in your last else statement:

else if(compare > 900.0);

So this line doesn't depend on the else clause and will always be executed:

stockWeight += 900;

Essentially you will always add 900 to the stockWeight immediately before the last print statement, whether or not compare > 900.0.

Upvotes: 5

Related Questions