取利率除以100(循环外,避免每次计算)。
然后,假设你的任期是几个月,你就可以循环计算条款的数目
:
$principal = $_REQUEST['principal'];
$interestRate = $_REQUEST['interestRate'] / 100;
$term = $_REQUEST['term'];
$month = array('January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September','October', 'November', 'December');
$time = 1 / 12; // Getting the "t" for the formula
for ($i = 0; $i < $term; $i++) { // Run the loop for the number of terms
$total = $principal * $interestRate * $time; // Total is interest formula (P * r * t)
$principal += $total; // Add total to principal to calculate for next month
echo "" . $month[$i] . ": Interest: " . $total . ", Principal: " . $principal . "
"; // Output month, interest earned, and new principal
}
?>
我为一个
10000
,利息
5
,和期限
五
是:
January: Interest: 41.666666666667, Principal: 10041.666666667
February: Interest: 41.840277777778, Principal: 10083.506944444
March: Interest: 42.014612268519, Principal: 10125.521556713
April: Interest: 42.189673152971, Principal: 10167.711229866
May: Interest: 42.365463457775, Principal: 10210.076693324
您可以设置输出格式。