I’m calculating how many percentage of your kills in my game was headshots… The calculation works as it should, but when I’m debugging the output with string.Format the output is not what I want it to be.
Let’s say I have 225 kills, 67 of those are headshots. The percent would then be 29.77778%. I want to use string.Format to only display “29.77%”, or maybe 29.7% but the output is still 29.77778% using this code:
headshotPercent = ((float)playerHeadshots / (float)playerKills);
headshotPercent = (headshotPercent * 100);
Debug.Log ("Headshot Percentage: " +
string.Format ("{0:P2}%", headshotPercent.ToString ()));
According to c# - Format decimal for percentage values? - Stack Overflow my string.Format should output 29.77% so I don’t really understand what’s wrong in my code…