Solved

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

Posted on 2013-06-29
9
319 Views
Last Modified: 2013-06-30
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...
0
Comment
Question by:tjyoung
  • 3
  • 3
  • 3
9 Comments
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 39287188
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...";
}

Open in new window

0
 
LVL 1

Author Comment

by:tjyoung
ID: 39287203
trying yours right now
0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 39287205
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);
}
?>

Open in new window

0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 1

Author Comment

by:tjyoung
ID: 39287210
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
 
LVL 58

Accepted Solution

by:
Gary earned 500 total points
ID: 39287211
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
 
LVL 58

Expert Comment

by:Gary
ID: 39287213
An increase from 2 to 3 is a 50% increase, not 33.3%.
0
 
LVL 1

Author Closing Comment

by:tjyoung
ID: 39287217
doh... it is 50% increase.
Thanks this works.
0
 
LVL 58

Expert Comment

by:Gary
ID: 39287222
:o)
You were working out the difference between the two numbers.
0
 
LVL 14

Expert Comment

by:Giovanni Heward
ID: 39289024
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

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
curl SSL certificate unable to get local issuer certificate 15 63
hasing a url 16 23
php checkbox validation 5 27
comma true 6 31
This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to count occurrences of each item in an array.

816 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now