Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.