Jump to content

[SOLVED] find difference between percentages


kaiman

Recommended Posts

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>)";
?>

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

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?

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.