Calculating a percentage with the result of a subtraction

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
FayeC
Forum Newbie
Posts: 4
Joined: Tue Apr 01, 2008 11:36 am

Calculating a percentage with the result of a subtraction

Post by FayeC »

Okie....
I have $variable1, $variable2 and $variable3.
My question is how to get $variable3 to show the percentage of the result of $variable2 - $variable1.
The values for $variable1 and $variable2 are going to be submitted via a form by the user and the result of the subtraction should be a % that will be assigned to $variable3. The percentage can be positive or negative.
For example:

$variable1 = 11.54
$variable2 = 10.45
$variable3 = -9.45%

Any help is appreciated.

FayeC
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Calculating a percentage with the result of a subtraction

Post by onion2k »

Code: Select all

$v1 = 11.54;
$v2 = 10.45;
$v3 = (($v1 - $v2)/$v1) * 100;
echo $v3;
 
You might want to round() $v3, or number_format() it or something.
Post Reply