Why does Processing say that 360/100 equals 3.0?

edited September 2016 in Programming Questions

Hi coders,

i have a math question: Why does Processing print the value 3here? Wouldn't 3.6 be the correct solution??

float n = 360/100;
println(n);
Tagged:

Answers

  • edited September 2016 Answer ✓
    • The expression 360/100 consists of a division operation w/ 2 integer operands.
    • In Java, when both operands are of a whole datatype, the result is always of a whole datatype too.
    • That is, any fractional part is removed from the result!
    • In order to fix that, make at least 1 of the operands become a fractional datatype.

    float n = 360/100.; // Notice the dot . at the end of 100!
    println(n); // 3.6
    exit();
    
  • Don't just use a dot, use .0 - a dot is to easy to overlook.

Sign In or Register to comment.