Mutley Posted April 10, 2007 Share Posted April 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/ Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 If total are 10 then 10 --------- 8 100 --------( ? ) 100*8/10 = 80 Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225751 Share on other sites More sharing options...
wildteen88 Posted April 10, 2007 Share Posted April 10, 2007 You'll want to do lost / won and not won / lost ((2 / * 100); Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225753 Share on other sites More sharing options...
obsidian Posted April 10, 2007 Share Posted April 10, 2007 You'll want to do lost / won and not won / lost ((2 / * 100); Actually, for a win percentage, you'll want to do won / total played like jitesh said <?php $winPct = (($win / $gamesPlayed) * 100); $losPct = (($loss / $gamesPlayed) * 100); ?> Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225757 Share on other sites More sharing options...
Mutley Posted April 10, 2007 Author Share Posted April 10, 2007 Thanks but why can't I divide by zero? I get an error saying I can't divide by zero. Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225796 Share on other sites More sharing options...
jitesh Posted April 10, 2007 Share Posted April 10, 2007 You can not devide by zero insted of you can keep a if condion like this if($game_playes == 0){ $per = 0; }else{ make calculations.... } Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225800 Share on other sites More sharing options...
Asheeown Posted April 10, 2007 Share Posted April 10, 2007 You can never divide by 0. Take a look at obsidians it's well thought out and dynamic, so those 3 variables: $win, $loss, and $gamesPlayed can all be different and $winPct and $losPct will be populated with your win and loss percentages. Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225815 Share on other sites More sharing options...
obsidian Posted April 10, 2007 Share Posted April 10, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225831 Share on other sites More sharing options...
utexas_pjm Posted April 10, 2007 Share Posted April 10, 2007 Combining Obsidian's and Jitesh's solutions yields: <?php $winPct = (($gamesPlayed == 0) ? 0 : ($win / $gamesPlayed) * 100); $lossPct = 100 - $winPct; ?> Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/46418-percentages/#findComment-225833 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.