简体   繁体   English

计算增加和减少的百分比

[英]Calculate the percentage of increase and decrease

anyone can write a PHP script as this calculator that calculates the percentage of increase and decrease?任何人都可以编写一个 PHP 脚本作为这个计算器来计算增加和减少的百分比?

http://www.marshu.com/articles/calculate-percentage-increase-decrease-percent-calculator.php http://www.marshu.com/articles/calculate-percentage-increase-decrease-percent-calculator.php

Thank you.谢谢你。

If you open the source of that page, you will find following:如果您打开该页面的源代码,您会发现以下内容:

function PercentIncrease(form) {
   var num1 = form.num1.value;
   var num2 = form.num2.value;
   return ((num2 - num1) / num1 * 100 + "%");
}

Let us know if you have problems with PHP syntax.如果您对 PHP 语法有疑问,请告诉我们。

Update: Obviously you have, so:更新:显然你有,所以:

function PercentIncrease($num1, $num2) {
    return (($num2 - $num1) / $num1 * 100 . "%");
}

$n1 = 10;
$n2 = 200;
echo PercentIncrease($n1, $n2); //output 1900%
echo PercentIncrease($n2, $n1); //output -95%

You could make a function like :你可以做一个像这样的功能:

function calcPercentInc($a, $b)
{
    return (100 * (($a / $b) - 1)) . '%';
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM