kaiman Posted October 9, 2009 Share Posted October 9, 2009 I am trying to figure out how to take a percentage of the total and return the remaining percentage i.e. 10% of 100% = 90% I have a variable called $percentage_needed that I am trying to subtract from another variable $percentage_received but I am a little stumped by the syntax for the operators. Can anyone help? Thanks a ton! kaiman Code below: <?php // total dollar amount of donations needed $total = "500.75"; // total dollar amount of donations received $donations = "20.45"; // total dollar amount of sales recieved $sales = "30.17"; // figure out percentage of total donations $percent1 = ($donations * 100) / $total; $percent2 = ($sales * 100) / $total; $percentage_needed = ; // format numbers for dollar amount $total = number_format($total, 2); $donations = number_format($donations, 2); $needed = number_format($needed, 2); $total_received = number_format($total_received, 2); $percent1 = number_format($percent1, 0); $percent2 = number_format($percent2, 0); $percentage_received = number_format($percentage_received, 0); $percentage_needed = number_format($percentage_needed, 0); // total dollar amount received $total_received = $donations + $sales; // total percentage received $percentage_received = $percent1 + $percent2; // total donations still needed $needed = $total - $donations - $sales; // total percent of donations recieved echo "Total expenses: $".$total."<br /> \n"; echo "Total donations: $".$donations." (<strong>".$percent1."%</strong>)<br /> \n"; echo "Total sales: $".$sales." (<strong>".$percent2."%</strong>)<br /><br /> \n"; echo "Total received: $".$total_received." (<strong>".$percentage_received."%</strong>)<br /><br /> \n"; echo "Amount needed: $".$needed." (<strong>".$percentage_needed."%</strong>)"; ?> Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/ Share on other sites More sharing options...
corbin Posted October 9, 2009 Share Posted October 9, 2009 I'm not sure exactly what you're trying to do, but it sounds like basic math: percent1 - percent2 Or, if you're trying to retain the weight of them: (val1+val2)/(max1+max2)*100 Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933513 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 corbin, Thanks for the reply. Not sure what you mean by "retain the weight" but what I am trying to due is figure out how to display the difference between the percentage donated and the percentage not yet donated, i.e. total percentage = 100%, percentage donated = 10%, percentage not donated = 90% (this is what I want to display). I have already calculated the percentage donated (10%), but how do I show the remaining percentage not yet donated (90%)? Thanks again, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933515 Share on other sites More sharing options...
corbin Posted October 9, 2009 Share Posted October 9, 2009 How would you do it on paper? 100% - 10% = 10 So... since the units are the same, it's just: 100-10 100-$percent Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933524 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 I'm not trying to get the 10%, I am trying to display the other 90% I just don't know the syntax/equation to do it. Thanks, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933528 Share on other sites More sharing options...
corbin Posted October 9, 2009 Share Posted October 9, 2009 100% - 10% = 10 Should have been 100%-10% = 90 (Technically 90%.) Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933534 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 Yep, the problem is that 1 is a $ dollar amount and one is a percentage % What is the PHP syntax for the equation to figure out how to subtract a percentage from the dollar amount and then convert it back to a percentage? Thanks, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933734 Share on other sites More sharing options...
Daniel0 Posted October 9, 2009 Share Posted October 9, 2009 That doesn't make sense (the "convert it back to percentage" part). Explain it in math and I'll port it to PHP Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933749 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 Okay, see my script so far for details. 1. I have a total amount needed: $500.75 2. I have one percentage that is purchases from the store they equal: $30.17 or 6% of $500.75 3. I have one percentage that is donations they equal: $20.45 or 4% of $500.75 4. The combined totals of $30.17 + $20.45 = $50.62 5. Of the total ($500.75) the combined totals in donations + sales ($50.62) = 10% 6. How do I create a function that subtracts the 10% ($50.62) from the total ($500.75=100%) and then convert it back to a percentage (90%) which can be displayed next to the total still needed ($500.75-$50.62 = $450.13) or as I want it displayed: Total still needed $450.13 (90%) The question is how do I subtract a percentage (10%) from the total ($500.75) and then display it as 90% so that when I update the other numbers the percentage of donations already and percentage of donations needed are reflective of each other? i.e. if one says 15% of donations received the other will say 85% of donations still needed... Does that make sense? Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933769 Share on other sites More sharing options...
Daniel0 Posted October 9, 2009 Share Posted October 9, 2009 Uh... taking a% off b is just (1-(a/100))*b. That's elementary mathematics. It's the same in PHP, just put $ before all variable names. So 10% off 500.75 is 0.9*500.75=450.68 Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933777 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 So how do I convert 90% or whatever percentage to a decimal in PHP? Because right now it is a number that will be changing as the percentage of donations change... Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933788 Share on other sites More sharing options...
Daniel0 Posted October 9, 2009 Share Posted October 9, 2009 Divide by 100. That's the definition of percentage; it means "per hundred". If you have 10 percent of something then you have 10 each time you have 100 of that something. Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933790 Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 $percentage_needed = ($total - ($donations + $sales ) ) / $total; Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933791 Share on other sites More sharing options...
johnsmith153 Posted October 9, 2009 Share Posted October 9, 2009 $percentage_needed = ($total - ($donations + $sales ) ) / $total; And then multiply the answer by 100 if you want the percentage amount. Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933792 Share on other sites More sharing options...
kaiman Posted October 9, 2009 Author Share Posted October 9, 2009 * SOLVED * Thanks john! My math is rusty and I was getting confused by the syntax and also forgetting the multiplication at the end. Now it seems to be working. I appreciate everyone's help, kaiman Quote Link to comment https://forums.phpfreaks.com/topic/177050-solved-find-difference-between-percentages/#findComment-933807 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.