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

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

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;

图片转代码服务由CSDN问答提供 功能建议

寻找以下代码的一些说明,用于计算两个不同数量之间的百分比变化,其中原始数据可能更大 或更小的数字。 那么这段代码是否可以显示增加+或减少 - 更改? 谢谢。

  $ original = 100; 
 $ current = 95; 
 
 $ percentChange =(1  -  $ original / $ current)* 100; 
 <  / code>  
 
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

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

    点赞 打赏 评论
  • douqiao2008 2017-10-03 13:39

    Yes, it will work for showing either a increase + or a decrease - change.

    点赞 打赏 评论

相关推荐 更多相似问题