<?php
/**
 * 複利終值表 計算
 * function compound_interest($money, $percent, $year)
 * var $money: 多少錢, ex: 1
 * var $percent: 幾%利息, ex: 1 (1%)
 * var $year: 幾年期, ex: 1 (1年)
 * example: compound_interest(100, 1, 10);
 *
 * return float
 */
function compound_interest($money$percent$year)
{
    for (
$i 0$i $year$i++)
        
$money += ($money $percent 100);

    
$money round($money3);

    return 
$money;
}

/**
 * 年金複利終值表 計算
 * function annuity_compound_interest($money, $percent, $year)
 * var $money: 多少錢, ex: 1
 * var $percent: 幾%利息, ex: 1 (1%)
 * var $year: 幾年期, ex: 1 (1年)
 * example: annuity_compound_interest(100, 1, 10);
 *
 * return float
 */
function annuity_compound_interest($money$percent$year)
{
    
$t = (pow(+ ($percent 100), $year) - 1) / ($percent 100) * $money;

    return 
round($t3);
}
?>
<!DOCTYPE html>
<html lang="zh-TW">
<head>
<meta charset="utf-8">
<title>複利計算</title>
<link rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/reset-fonts-grids/reset-fonts-grids.css" type="text/css">
<link rel="stylesheet" href="http://yui.yahooapis.com/2.9.0/build/base/base.css" type="text/css">
<style>
table td {text-align:right;}
</style>
</head>
<body>
<h1>複利終值表 - 1元的複利終值</h1>
<table>
    <tr>
        <td>期數(年) / 每期利率</td>
        <?php
        
for ($i 1$i <= 30$i++) {
            echo 
"<td>$i%</td>";
        }
        
?>
    </tr>
    <?php
    
for ($i 1$i <= 50$i++) {
        echo 
'<tr>';
        echo 
"<tr><td>$i</td>";
        for (
$j 1$j <= 30$j++) {
            echo 
'<td>' compound_interest(1$j$i) . "</td>\n";
        }
        echo 
"</tr>\n";
    }
    
?>
</table>

<h1>年金複利終值表 - 每期1元的年金複利終值</h1>
<table>
    <tr>
        <td>期數(年) / 每期利率</td>
        <?php
        
for ($i 1$i <= 30$i++) {
            echo 
"<td>$i%</td>";
        }
        
?>
    </tr>
    <?php
    
for ($i 1$i <= 50$i++) {
        echo 
'<tr>';
        echo 
"<tr><td>$i</td>";
        for (
$j 1$j <= 30$j++) {
            echo 
'<td>' annuity_compound_interest(1$j$i) . "</td>\n";
        }
        echo 
"</tr>\n";
    }
    
?>
</table>

</body>
</html>