Skip to content
Advertisement

Calculating The Percentage Change Between Two Numbers

Looking for some clarification on below code for calculating the percentage change between two different number of which the original could be a greater or smaller number. So will this code work for showing either a increase + or a decrease – change? Thanks.

$original= 100;
$current = 95;

$percentChange = (1 - $original / $current ) * 100;

Advertisement

Answer

Find difference and then count percentage like this

<?php
    $original= 100;
    $current = 115;
    $diff = $current - $original;
    $more_less = $diff > 0 ? "More" : "Less";
    $diff = abs($diff);
    $percentChange = ($diff/$original)*100;
    echo "$percentChange% $more_less agaist $original";
?>

Difference will be same for 110 and 90 against 100

Live demo : https://eval.in/872926

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement