I am trying to get percentage but the result is error, i have expression as:
Uper=(Upcount/total)*100;
where Uper
is float while Upcount
and total
is integer i am getting the result Uper=0
.
I am trying to get percentage but the result is error, i have expression as:
Uper=(Upcount/total)*100;
where Uper
is float while Upcount
and total
is integer i am getting the result Uper=0
.
An int
divided by an int
will result in an int
. That could be 0
. Multiply 0 * 100
, convert to float
, and the result is still 0.0
. You need at least one of the operands to be floating point before the division will give a floating point result.
Try:
Uper = ((float)Upcount/(float)total)*100.0;
The extra (float)
is me being paranoid that this line might be modified in the future without fully understanding the floating-point requirement. The 100.0
is to be explicit about what you want -- a floating point result.
Perhaps changing Upcount
or total
to float
would make more sense.
the division of 2 integers will always result in an integer which is 0 in your case.
To solve this, use the following code:
Uper = ((Double) Upcount) / total * 100
Casting at least 1 member to Double or Float will get the result you want