dongmo1708 2017-10-03 13:36
浏览 1023
已采纳

计算两个数字之间的百分比变化

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;
  • 写回答

2条回答 默认 最新

  • drti52047 2017-10-03 13:42
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?