Possible Duplicate:
Division returns zero
simple calculation not working for some reason
Why is this:
int prozent = (totalkcals / 2000) * 100;
returning 0?
totalkcals is in this case 567, I double-checked it.
Possible Duplicate:
Division returns zero
simple calculation not working for some reason
Why is this:
int prozent = (totalkcals / 2000) * 100;
returning 0?
totalkcals is in this case 567, I double-checked it.
This is because of integer division
The division rounds the result towards zero, and the absolute value of the result is the largest possible integer that is less than the absolute value of the quotient of the two operands. The result is zero or positive when the two operands have the same sign and zero or negative when the two operands have opposite signs
Its because of Integer division.
double prozent = ((double)totalkcals / 2000) * 100;
567 / 2000 = 0.2835, which when cast to an int (as specified by your data type) becomes zero. 0 * 100 = 0.
So: double prozent = ((double)totalcals / 2000.0) * 100.0;
Its because its an integer calculation, so the operation from (totalkcals / 2000) returns 0;