Literal Values and Literal Arithmatic
There are a couple of issues with that code. Firstly, non-floating point literal values are of type int
by default and so 3004230
in your code is an int
. To explicitly declare it a long
use 3004230L
instead.
Also, all arithmetic done with non-floating point literals returns an int
result unless one of the variables are casted specifically to a floating point type such as float
or double
. As such (a/b)*100
is less than 1, and therefore is truncated down to 0 (the floating point values are just cut off). Also, even if it did return the same result you are trying to store it in a long
which can not store floating point values.
So, you should do something like the following to get the real result.
long a = 3004230L; // Use the L notation to declare this literal a long.
long b = 6793368L;
double c = ((double) a/b)*100; /* casting one of the variables to (double) means the result will not be 0 */
I hope this helps.