Page 1 of 1

Calculating a percentage with the result of a subtraction

Posted: Thu Apr 03, 2008 12:46 pm
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

Re: Calculating a percentage with the result of a subtraction

Posted: Thu Apr 03, 2008 1:13 pm
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.