?
Solved

PHP Math equation problem

Posted on 2011-03-02
6
Medium Priority
?
371 Views
Last Modified: 2012-05-11
Hi - I'm programming in PHP.  Here is some YTD information:
2011 YTD 14,989.3 2,877,688.8 $73,101.97
2010 YTD 18,409.8 1,911,203.4 $52,875.36
YTD Change Down 1.228% Up 1.506% Up 1.383%

The YTD Change % looks incorrect to me.  This is the formula I'm using to calculate the percentage.  I know you mathematical people will be horrified but I never did very well in math. :(  Help!

Thanks - Zack
if ($LMTDYYYAVG100 > $CMTDYYYAVG100) { $chMTDAVG100="<font color=red>Down " . number_format(($LMTDYYYAVG100/$CMTDYYYAVG100)*1, 3) . "%</font>"; } else { $chMTDAVG100="<font color=green>Up " . number_format(($CMTDYYYAVG100/$LMTDYYYAVG100)*1, 3) . "%</font>"; }
if ($LMTDYYYJet > $CMTDYYYJet) { $chMTDJet="<font color=red>Down " . number_format(($LMTDYYYJet/$CMTDYYYJet)*1, 3) . "%</font>"; } else { $chMTDJet="<font color=green>Up " . number_format(($CMTDYYYJet/$LMTDYYYJet)*1, 3) . "%</font>"; }
if ($LMTDYYYSPI > $CMTDYYYSPI) { $chMTDSPI="<font color=red>Down " . number_format(($LMTDYYYSPI/$CMTDYYYSPI)*1, 3) . "%</font>"; } else { $chMTDSPI="<font color=green>Up " . number_format(($CMTDYYYSPI/$LMTDYYYSPI)*1, 3) . "%</font>"; }

Open in new window

0
Comment
Question by:zack4x4
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
6 Comments
 
LVL 37

Accepted Solution

by:
TommySzalapski earned 2000 total points
ID: 35023968
The numbers are actually right, just the interpretation is off.
1.228 means you have currently 1.228 times last year, so that's a 22.8% increase.
If you get .9 then that's a 10% decrease. Take your number, subtract 1 and multiply by 100.
So (ThisYear/LastYear - 1)*100 gives the percentage change.
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35023977
For down, the formula is a little reversed. Basically, you just subtract from 1 instead of subtracting 1.
(1 - ThisYear/LastYear)*100
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35023984
I'll attempt to fix the code to show what I mean (I'm not the best at PHP so here goes):
if ($LMTDYYYAVG100 > $CMTDYYYAVG100) { $chMTDAVG100="<font color=red>Down " . number_format((1 - $LMTDYYYAVG100/$CMTDYYYAVG100)*100, 3) . "%</font>"; } else { $chMTDAVG100="<font color=green>Up " . number_format(($CMTDYYYAVG100/$LMTDYYYAVG100 - 1)*100, 3) . "%</font>"; }
if ($LMTDYYYJet > $CMTDYYYJet) { $chMTDJet="<font color=red>Down " . number_format((1 - $LMTDYYYJet/$CMTDYYYJet)*100, 3) . "%</font>"; } else { $chMTDJet="<font color=green>Up " . number_format(($CMTDYYYJet/$LMTDYYYJet - 1)*100, 3) . "%</font>"; }
if ($LMTDYYYSPI > $CMTDYYYSPI) { $chMTDSPI="<font color=red>Down " . number_format((1 - $LMTDYYYSPI/$CMTDYYYSPI)*100, 3) . "%</font>"; } else { $chMTDSPI="<font color=green>Up " . number_format(($CMTDYYYSPI/$LMTDYYYSPI - 1)*100, 3) . "%</font>"; }

Open in new window

0
WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

 
LVL 4

Author Closing Comment

by:zack4x4
ID: 35024313
That worked perfectly!!  Thanks!!  Just one question why do you have to -1 (subtract 1) from the back or from the front?  What does it do to the answer?

Apprecaite it!
Thanks!
0
 
LVL 37

Expert Comment

by:TommySzalapski
ID: 35028455
The number you generate is the ratio of the new number to the old number. To see the change, you subtract the old number. It's the same as (new - old)/old. With a little algebra, you can see that they are identical.
If you lost 10%, this would actually give you a value of -10% gain. But you want to show the number as positive, so we flip it - (new - old)/old = (-new + old)/old = (old - new)/old = 1 - new/old.
I just short cut the algebra.
0
 
LVL 4

Author Comment

by:zack4x4
ID: 35032400
Thanks for the explanation! I'm horrific in algebra. :(

Zack
0

Featured Post

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
This article provides a brief introduction to tissue engineering, the process by which organs can be grown artificially. It covers the problems with organ transplants, the tissue engineering process, and the current successes and problems of the tec…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

770 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