# Simple PHP math problem trying to get a percentage and failing at it.

I'm sorry to have to ask. I should've listened up in math class:

I'm comparing open rates for 2 different email campaigns.

For example:
\$current_campaign = 3
\$previous_campaign = 2

So I'm trying to do the math to show what percentage of increase or decrease the current campaign has compared to the previous. Seemed simple enough when I started....

My math is:
\$sent_percent = number_format((\$previous_campaign / \$current_campaign) * 100 - 100,0);
echo \$sent_percent."%";

This echos out: -33% where based on the above it should actually be a 33% increase (no negative sign)

If I take out the '-100' in the formula, I get 67% but that isn't how much of an improvement the current campaign is compared to the previous. See what I mean?

This can't be that hard and I'm chalking it up to my inexperience and failure to take math seriously in high school.

Help...
LVL 1
###### Who is Participating?

x

Commented:
x66's calculations are wrong.

Initial figure / 100 - gives you what 1% represents (of the old campaign)
New figure divide by the (1%) - gives you the percentage of the new value (campaign)
Take away 100 gives you the actual increase/decrease.

So in math
\$diff = (newcampaign / (oldcampaign/100)) - 100
0

Commented:
Try...

``````\$current_campaign = 3;
\$previous_campaign = 2;
\$sent_percent = round(( \$current_campaign - \$previous_campaign ) / abs( \$current_campaign ) * 100, 2);
if (preg_match('/\A(\d+.\d{2})\Z/', \$sent_percent)) {
# Successful match
echo "\$sent_percent%";
} else {
# Match attempt failed
echo "Error...";
}
``````
0

Author Commented:
trying yours right now
0

Commented:
Or...

``````<?php
\$current_campaign = 3;
\$previous_campaign = 2;
\$sent_percent = round( ( \$current_campaign - \$previous_campaign ) /
abs( \$current_campaign ) * 100, 0 );
if (preg_match('/\A(\d+)\Z/', \$sent_percent)) {
# Successful match
echo "\$sent_percent%";
} else {
# Match attempt failed
echo "Error...";
unset(\$sent_percent);
}
?>
``````
0

Author Commented:
Hi,
The last version works fine when the current_campaign is a larger number.
I get the error when its less though. Can't figure out what the preg_match is doing?
0

Commented:
An increase from 2 to 3 is a 50% increase, not 33.3%.
0

Author Commented:
doh... it is 50% increase.
Thanks this works.
0

Commented:
:o)
You were working out the difference between the two numbers.
0

Commented:
Good catch.

If you switch my variables around you get the gain; just another way to do it.

<?php
\$current_campaign = 3;
\$previous_campaign = 2;
\$diff = round( ( \$current_campaign - \$previous_campaign ) /
abs( \$current_campaign ) * 100, 0 );
\$gain = round( abs( ( \$previous_campaign - \$current_campaign ) /
\$previous_campaign ) * 100, 0 );
echo "diff: \$diff% | ";
echo "gain: \$gain%";
?>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.