Jump to content

Percentages


Mutley

Recommended Posts

This is probably really basic but I can't figure it out.

 

If I have someone who has played 10 times, 8 times he has won and 2 times he has lost.

 

What I want to do is make a % for this (in this case it should be 80%). If I do:

(8/2)*100 = 400%

 

So how do I get it to work the way I want? Basic maths I fail at.  :(

Link to comment
https://forums.phpfreaks.com/topic/46418-percentages/
Share on other sites

Once again, like jitesh says, mathematically, you cannot divide by zero. This is not a PHP thing, this is a math thing. Try it out: run calc on your PC and do something like 3 / 0. You'll get the same error message. you've got to account for that in your logic. So, combining the recommendation that jitesh said with my above code, I'd recommend something like this:

<?php
if ($gamesPlayed > 0) {
  $winPct = (($win / $gamesPlayed) * 100);
  $losPct = (($loss / $gamesPlayed) * 100);
} else {
  $winPct = 'n/a';
  $losPct = 'n/a';
}
?>

 

Good luck.

Link to comment
https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225831
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.