It seems as if you are using int
s for the operands.
With int
division, you must remember that the decimal part of the result gets truncated.
I.e., for the line (304/1000)*100
(replacing the variables with their values for simplicity), the following is calculated:
304
is divided by 1000
, which results in 0.304
. Then, the decimal is truncated, since this is integer division, resulting in 0
being the end result.
Then, 0
is multiplied by 100
, which gives 0
as the result.
Solution:
Make the operands decimal
s, float
s, or double
s. (100m
, 100f
, and 100d
, respectively, for an example of each type)
These three types support decimals, so if you use them, you will get the correct result of 30.4
.
Either that, or you can multiply one of the values in the parentheses by 1.0
of any of the three above types:
(1.0f * 304 / 1000) * 100