Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/245.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP函数递归地将百分比应用于金额_Php_Logic_Mathematical Optimization - Fatal编程技术网

PHP函数递归地将百分比应用于金额

PHP函数递归地将百分比应用于金额,php,logic,mathematical-optimization,Php,Logic,Mathematical Optimization,所以我试图创建一个函数,它接受amount、percentage(decimal)和times,并返回一个包含amount的double 我预期的结果如下: $amount = 10000 $percentage = 1.1 $times = 1 所以 elevateToPercentage($amount,$percentage,$times)=10000*1.1=11000 $times=2 电梯运营百分比($amount,$percentage,$times)=((10000*1.1)*

所以我试图创建一个函数,它接受amount、percentage(decimal)和times,并返回一个包含amount的double

我预期的结果如下:

$amount = 10000
$percentage = 1.1
$times = 1
所以

elevateToPercentage($amount,$percentage,$times)=10000*1.1=11000
$times=2
电梯运营百分比($amount,$percentage,$times)=((10000*1.1)*1.1)=12100
$times=4
电梯运营成本($amount,$percentage,$times)=((((10000*1.1)*1.1)*1.1)*1.1)=14641
私有函数elevateToPercentage($amount,$percentage,$times){
$count=0;

对于($a=0;$a您可以使用pow函数实现它

function elevateToPercentage($amount, $percentage, $times) {
    $multiple = pow($percentage, $times);
    return number_format($amount*$multiple) ;
}
$amount = 10000;
$percentage = 1.1;
$times = 1;
echo elevateToPercentage($amount, $percentage, $times);
输出:

$times = 1; 11,000
$times = 2;  12,100
$times = 4; 14,641

您可以使用pow函数来实现它

function elevateToPercentage($amount, $percentage, $times) {
    $multiple = pow($percentage, $times);
    return number_format($amount*$multiple) ;
}
$amount = 10000;
$percentage = 1.1;
$times = 1;
echo elevateToPercentage($amount, $percentage, $times);
输出:

$times = 1; 11,000
$times = 2;  12,100
$times = 4; 14,641
那么:

function elevateToPercentage($amount, $percentage, $times) {
    if ($times == 1){
        return $amount * $percentage;
    }else{
        return $percentage * elevateToPercentage($amount, $percentage, $times -1);
    }
}
那么:

function elevateToPercentage($amount, $percentage, $times) {
    if ($times == 1){
        return $amount * $percentage;
    }else{
        return $percentage * elevateToPercentage($amount, $percentage, $times -1);
    }
}


$a>=$times;
?!?你的意思肯定是
@MarkBaker修复了这个例子。谢谢,看到我仍然没有做我需要做的事情了吗?@IgnacioVazquez Abrams在每个循环中存储从操作中获得的计数。我不知道你想说什么。
$a>=$times;
你的意思是说,
@MarkBaker修复了这个例子。谢谢,看到了吗,我累了xD仍然没有做我需要做的事情,
$count
是做什么的?@IgnacioVazquez Abrams在每个循环中存储从操作中得到的计数。我不知道你想说什么。就是这样。我不知道这个函数。谢谢我知道。我不知道这个函数。谢谢!这也很好,兄弟!谢谢!我想把两个答案都标记为正确。Ravinder的答案也是正确的,但是如果目标是使用递归函数,我的方法更好。如果你不需要使用递归方法(比如如果这不是家庭作业“使用递归函数…”),Ravinder的方法对大多数人来说可能更具可读性。顺便说一句,如果是家庭作业,你应该处理$times<1时会发生什么。更多信息,请参阅。我了解递归,只是不知道如何执行此特定函数。不,这不是家庭作业xD Cheers这也很好,兄弟!谢谢!我想两者都标记答案是正确的。Ravinder的答案也是正确的,但是如果目标是使用递归函数,我的方法更好。如果你不需要使用递归方法(就像这不是家庭作业“使用递归函数…”),Ravinder的方法对大多数人来说可能更具可读性。顺便说一句,如果是家庭作业,你应该处理$times<1时会发生的事情。有关更多信息,请参阅。我了解递归,只是不知道如何执行此特定函数。不,这不是家庭作业xD干杯