dongtan1845 2014-10-15 10:32
浏览 16
已采纳

计算php中的复合兴趣

Compliments all, Am quite new in working with php but i have to try somehow... My problem is this, the formular for compound interest is A = P(1 + r/n) ^ nt Now, if i got $60000 to invest for 1 years at 15%, my interest gained would be $9000, and if i add it to my initial $60000 therefore the final amount = $69000. Here's the catch. if the year is more than 1yr like 2yrs, i want to add my initial ($60000) to the final amount ($69000) to get a new value = $129000 which i will multiply with 15% to get a new interest value = $19350 and add it to the new value ($129000) to get a new amount = $148350. and this will continue for the number of years specified.

from the example above year 1 = $60000 + $9000 = $69000 year 2 = $69000 + $60000 = $129000 ($129000*15%=$19350) now $129000+$19350 =$148350 year 3 = $148350 + $60000 = $208350 ($208350*15%=$31252.50) now $208350+$31252.50 =$239602.50 etc

i believe my explanation is plain enough. thank you.

  • 写回答

1条回答 默认 最新

  • duanou2016 2014-10-15 11:05
    关注

    Easiest way is to create a recursive function, assuming same yearly investment

    <?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;
        }
    
    ?>
    

    Then to run the function according to form input

    <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)."
    ";
    }
    ?>
    

    And the form input

    <?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>
    

    EDIT: edited to include example of running function with a form input

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记