timmah1 Posted June 18, 2008 Share Posted June 18, 2008 ok, i've been at this for two days now, and it's not working. I'm trying to find a percentage of 3 numbers. Here's an example of what I am doing $total = 2+3; $total_overall = 6; $group1 = $total * $total_overall; $group2 = $group1 % 100; $groups = number_format($group2, 0); If $total_overall is equal to $total, it should be 100% If $total_overall is less than $total, it should give me a percentage under 100% If $total_overall is greater than $total, it should stay at 100% For some reason, if $total_overall is more than $total, it gives me a percentage under 100%. Can anybody help me out here? Thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/ Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 100 / tot * mark thats how to calc percent, % is modulus in programming, divide and give remainder... Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568440 Share on other sites More sharing options...
timmah1 Posted June 18, 2008 Author Share Posted June 18, 2008 thanks for the reply rarebit, but I have to be honest, that makes no sense to me Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568443 Share on other sites More sharing options...
Jabop Posted June 18, 2008 Share Posted June 18, 2008 <?php echo (($total / $total_overall) * 100) . "%"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568452 Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 thanks for the reply rarebit, but I have to be honest, that makes no sense to me What doesn't make sense to you? The basic percent formula is (max/number)*100 (technically grouping isn't needed there) like someone already said. Or, do you mean what is modulus? 5 mod 2 for example is 1, since the remainder of 5/2 is 1. Modulus simply takes the 'remainder'.... 20 % 5 = 0 20 % 6 = 2 20 % 11 = 9 So on.... 100%20 = 0 100%21 = 19 Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568460 Share on other sites More sharing options...
timmah1 Posted June 18, 2008 Author Share Posted June 18, 2008 jabop, That works perfect except for one thing. If overall total is higher than 100, it goes more than 100 $total = 3+2; $over_total = 6; echo (($total / $total_overall) * 100) . "%"; It shows 101% How do I limit it to go no higher than 100? Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568462 Share on other sites More sharing options...
ohdang888 Posted June 18, 2008 Share Posted June 18, 2008 variable error: $over_total = 6; LOOK AT THIS, IT SAYS "OVER_TOTAL", WHILE THE NEXT LINE SAYS "TOTAL_OVERALL" echo (($total / $total_overall) * 100) . "%"; Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568467 Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 No offense timmah1, but you're either new to PHP, or... wow.... What do you want to do? If the percent is over 100%, make it 100%. How would one do that? Phrase it in PHP. //if the number is over 100, make it 100. if($number > 100) $number = 100; Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568471 Share on other sites More sharing options...
timmah1 Posted June 18, 2008 Author Share Posted June 18, 2008 I'm not new to PHP corbin, I just didn't know the correct way. Now that you have explained everything to me, it makes perfect sense. Thank you Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568472 Share on other sites More sharing options...
rarebit Posted June 18, 2008 Share Posted June 18, 2008 e.g. 1: (tot / mark) * 100 (20/10)*100 = 200 e.g. 2: 100 / tot * mark 100 / 20 * 10 = 50 Am I missing something? Which of those calculates percentage? Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568477 Share on other sites More sharing options...
corbin Posted June 18, 2008 Share Posted June 18, 2008 Oh crap.... I said it backwards.... I just had an idiot moment... (max/number)*100 Should've been... (number/max)*100 Sorry bout that x.x. Quote Link to comment https://forums.phpfreaks.com/topic/110788-solved-getting-a-percentage/#findComment-568499 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.