System.out.println(300 + ((160 - 300) / (240 - 20)) * (27 - 20));
//Should print 295.5 but prints 300
System.out.println(300 + ((160 - 300) / (240 - 20)) * (26 - 20));
//Should print 296.1 but prints 300
I can’t seem to figure out what’s going wrong with this simple equation. I’ve tried storing the variable in ints, doubles, and floats then printing it out, and I still get 300.
I have put the equation into different calculators like Google and http://web2.0calc.com/, and they produce the correct answers.
Please help me.
Cast one of the divide operands to
double, and thedoubletype will propagate up through the calculation:Further reading