- Home /
Question by
Cjmarkham · May 30, 2014 at 11:51 AM ·
calculationspercentage
Percentage calculation returning 0
I am trying to calculate a percentage based on shots/kills.
I have the following but it will either return 100 (shots == kills) or 0 (shots > kills).
public static int shotsFired = 0;
double accuracy = (kills / shotsFired) * 100;
For this example kills = 5 and shots = 6 which should equal 83.3. So why does it return 0?
Note that this isnt the full code and the shotsFired
variable is declared outside of the function where accuracy is calculated.
Comment
Best Answer
Answer by Nerevar · May 30, 2014 at 11:57 AM
Hi
it returns 0 because you need to convert kills and shotsFired in double.
5/6*100=0 but 5.0/6.0*100 = 83.3
Answer by Kusakubari · May 01, 2019 at 11:14 PM
if using int's - cast them as floats...
float perc = ((float)count / (float)countLength * 100); // without float would return zer0