计算php中的复合兴趣

时间:2014-10-15 10:32:40

标签: php

赞美所有, 在使用php方面我很新,但我必须尝试某种方式...... 我的问题是这个, 复利的公式是A = P(1 + r / n)^ nt 现在,如果我有6万美元投资1年15%,我的兴趣是9000美元,如果我把它加到我的初始$ 60000,因此最终金额= $ 69000。 这是捕获。如果年份超过1年像2yrs,我想将我的初始值($ 60000)添加到最终金额($ 69000)以获得新值= $ 129000我将乘以15%以获得新的利息值= $ 19350并添加它获得新价值($ 129000)以获得新的金额= $ 148350。这将持续指定的年数。

  

来自上面的例子   第1年= $ 60000 + $ 9000 = $ 69000   第2年= $ 69000 + $ 60000 = $ 129000($ 129000 * 15%= $ 19350)现在$ 129000 + $ 19350 = $ 148350   第3年= $ 148350 + $ 60000 = $ 208350($ 208350 * 15%= $ 31252.50)现在$ 208350 + $ 31252.50 = $ 239602.50   等

我相信我的解释很明确。 谢谢。

3 个答案:

答案 0 :(得分:2)

最简单的方法是创建一个递归函数,假设每年投资相同

<?php

function interest($investment,$year,$rate=15,$n=1){
    $accumulated=0;
    if ($year > 1){
            $accumulated=interest($investment,$year-1,$rate,$n);
            }
    $accumulated += $investment;
    $accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
    return $accumulated;
    }

?>

然后根据表单输入

运行该函数
<html>
<head><title>Calculate Compound Interest</title></head>
<body><h3>Calculate Compound Interest</h3>

<?php
$initial=0;
$years=0;
$rate=15;
$n=1;

if (isset($_POST['initial'])){$initial=$_POST['initial'];}
if (isset($_POST['years'])){$years=$_POST['years'];}
if (isset($_POST['rate'])){$rate=$_POST['rate'];}
if (isset($_POST['n'])){$n=$_POST['n'];}

if (isset($_POST['initial'])){
    echo "After ". $years. " years, the accumulated interest is ". interest($initial,$years,$rate,$n)."\n";
}
?>

表单输入

<?php echo "<form method=\"post\" action=\""  . $_POST['SELF.PHP'] . "\">"; ?>

<p>Initial amount (contribution), $: <?php echo '<input type="number" name="initial" value='. $initial.' required/>'?> </p>
<p> Annual interest rate : <?php echo '<input type="number" name="rate" value='.$rate.' />' ?> % </p>
<p> Number of compounding periods per year? <?php echo '<input type="number" name="n" value='.$n.' />'?> </p>
<p> How many years? <?php echo '<input type="number" name="years" value='. $years.' min="1" required/>' ?></p>
<p> <input type="submit" value="Generate compound interest table."/> </p>
</form> </body> </html>

编辑:编辑以包含带有表单输入的运行函数的示例

答案 1 :(得分:1)

首先您将帖子值设为

<?php
$principal = $_POST['principal'];
$year = $_POST['year'];
$interest = $_POST['interest'];
?>

您可以通过考虑小数定义函数来计算:

<?php

function compound_interest($principal, $year, $interest){
    $converted_interest = $interest / 100;
    $completed_interest = $converted_interest + 1;
    $exponent = pow($completed_interest,$year);
    $final = $principal * $exponent;
    return number_format((float)$final, 2, ',', '.');
}

?>

最后,运行函数:

<?php $calculate = compound_interest($principal, $year, $interest); echo $calculate; ?>

答案 2 :(得分:0)

找到下面升级的一个固定多年

<?php
// PHP program to find
// compound interest for
// given values.

$years = 10; // number of years
$initial = 10000; // Principal amount
$rate = 10; // Rate of Interest
$n =4; // number of compounding in a year

function CompoundInterest($investment, $year, $rate=10, $n=1)
{
    $accumulated=0;
    if ($year > 1)
    {
        // print_r($investment);
        $accumulated = CompoundInterest($investment, $year-1 , $rate, $n);
    }else {
        $accumulated += $investment;
    }

    $accumulated = $accumulated * pow(1 + $rate/(100 * $n),$n);
    return $accumulated;
}

$totalMaturity = CompoundInterest($initial,$years,$rate,$n);
echo "Deposit Amount is ". $initial."\n";
echo '<br>';
echo "After ". $years. " years, the accumulated interest is ". $totalMaturity."\n";
echo '<br>';
echo "After ". $years. " years, Interest is ". ($totalMaturity - $initial) ."\n";

?>
相关问题