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
: 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.
adverse, gloomy, pessimistic, unfavorable, weak, abrogating, annulling, anti, con, contrary, contravening, denying, disallowing, disavowing, dissenting, gainsaying, impugning, invalidating, jaundiced, naysaying.
This is pretty basic math.
% difference from x to y is 100*(y-x)/x
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.
for x - y
the percentage is (y-x)/x*100
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With