Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Negative & Positive Percentage Calculation

Tags:

math

Let's say i have 3 sets of numbers and i want the % of their difference.

30 - 60
94 - 67
10 - 14

I want a function that calculate the percentage of the difference between each 2 numbers, and the most important is to support positive and negative percentages.

Example:

30 - 60 : +100%
94 - 67 : -36% ( just guessing )
10 - 14 : +40%

Thanks

like image 770
CodeOverload Avatar asked Dec 08 '10 21:12

CodeOverload


People also ask

What is the meaning of being negative?

: lacking positive qualities. especially : disagreeable. a colorless negative personality. : marked by features of hostility, withdrawal, or pessimism (see pessimism sense 1) that hinder or oppose constructive treatment or development. a negative outlook.

What is a better word for negative?

adverse, gloomy, pessimistic, unfavorable, weak, abrogating, annulling, anti, con, contrary, contravening, denying, disallowing, disavowing, dissenting, gainsaying, impugning, invalidating, jaundiced, naysaying.


4 Answers

This is pretty basic math.

% difference from x to y is 100*(y-x)/x

like image 123
The Archetypal Paul Avatar answered Oct 26 '22 08:10

The Archetypal Paul


The important issue here is whether one of your numbers is a known reference, for example, a theoretical value.

With no reference number, use the percent difference as

100*(y-x)/((x+y)/2)

The important distinction here is dividing by the average, which symmetrizes the definition.

From your example though, it seems that you might want percent error, that is, you are thinking of your first number as the reference number and want to know how the other deviates from that. Then the equation, where x is reference number, is:

100*(y-x)/x

See, e.g., wikipedia, for a small discussion on this.

like image 31
tom10 Avatar answered Oct 26 '22 10:10

tom10


for x - y the percentage is (y-x)/x*100

like image 31
Gabi Purcaru Avatar answered Oct 26 '22 09:10

Gabi Purcaru


Simple math:

function differenceAsPercent($number1, $number2) {
    return number_format(($number2 - $number1) / $number1 * 100, 2);
}

    echo differenceAsPercent(30, 60); // 100
    echo differenceAsPercent(94, 67); // -28.72
    echo differenceAsPercent(10, 14); // 40
like image 35
Darryl E. Clarke Avatar answered Oct 26 '22 08:10

Darryl E. Clarke