สวัสดีครับ วันนี้เราจะมาแนะนำการเขียนโปรแกรม คำนวณดอกเบี้ยแบบ ลดต้นลดดอก ด้วย สูตร PMT กันครับ
โดยสูตร PMT จะทำการหา จำนวนที่เราต้องจ่ายในแต่ละเดือน ให้ เท่า ๆ กันครับเช่น ยอดเงินกู้ 10,000 บาท ระยะเวลาผ่อนชำระ 12 เดือน อัตราดอกเบี้ย 28% ต่อปี เมื่อใช้สูตร PMT คำนวนออกมาแล้วก็จะได้ ยอดที่ต้องชำระต่อเดือน คือ 965 บาทต่อเดือน เป็นต้น
โดยวิธีการคำนวณ ในบทความนี้จะใช้ข้อมูลจาก
https://www.1213.or.th/th/serviceunderbot/loans/Pages/effectiverate.aspx
เรามาเริ่มกันเลย ก่อนอื่น เรามาดูสูตรการคำนวณแบบ PMT กันก่อนเลยดีกว่าครับ
ขอขอบคุณภาพจาก https://www.1213.or.th
เมื่อเรารู้สูตรการคำนวณ หาค่า PMT แล้ว เรามาเขียน โปรแกรมกันต่อเลยครับ
โดยเราจะใช้ข้อมูลจากต้วอย่างด้านบนในการคำนวณ และเขียนเป็นโปรแกรม PHP ได้ดังนี้ครับ
<?php $principal = 12000; $payMonth = 6; /* วิธีคำนวณหาดอกเบี้ยต่อปี ตัวอย่าง 24% จะเท่ากับ (24/100)/12 = 0.02 */ $int = 0.02; //24% ต่อปี $result1 = (1/pow((1+$int),$payMonth)); $result2 = (1-$result1)/$int; $pmt = $principal/$result2; echo $resultPmt = number_format($pmt, 2, '.', ''); ?>
ผลการทำงานจะได้ ผลลัพท์เท่ากับ 2142.31
ต่อมาเรามาลองทำตารางแจกแจงว่าในแต่ละเดือน เราเสีย ดอกเบี้ย หรือ ชำระเงินต้น ไปเท่าไหร่ได้ดังนี้ครับ
<?php
$principal = 12000;
$payMonth = 6;
$int = (28/100)/12; //28%ต่อปี
$result1 = (1/pow((1+$int),$payMonth));
$result2 = (1-$result1)/$int;
$pmt = $principal/$result2;
$resultPmt = number_format($pmt, 2, '.', '');
?>
<!doctype html>
<html lang="en">
<head>
<!-- Required meta tags -->
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<!-- Bootstrap CSS -->
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
<title>PMT</title>
</head>
<body>
<div class="container">
<table class="table table-bordered">
<tr class="text-center">
<th>งวดที่</th>
<th>ยอดหนี้</th>
<th>ยอดชำระต่อเดือน</th>
<th>ชำระดอกเบี้ย</th>
<th>ชำระเงินต้น</th>
<th>ยอดหนี้คงเหลือ</th>
</tr>
<?php
for($i = 1;$i<=$payMonth;$i++){
//สำหรับเดือนสุดท้าย นำดอกเบี้ยมารวมใน ยอดชำระต่อเดือน
if($payMonth == $i){
$resultPmt = $principal + $calInterest;
$last_balance = $resultPmt;
}
$calInterest = (($principal * 0.28)/12);
$payPrincipal = $resultPmt - $calInterest;
echo "<tr class='text-right'>";
echo "<td class='text-center'>".$i."</td>";
echo "<td>".number_format($principal, 2, '.', '')."</td>";
echo "<td>".number_format($resultPmt, 2, '.', '')."</td>";
echo "<td>".number_format($calInterest, 2, '.', '')."</td>";
echo "<td>".number_format($payPrincipal, 2, '.', '')."</td>";
$principal = $principal - $payPrincipal;
//สำหรับเดือนสุดท้าย (นำดอกเบี้ยมารวม + ยอดชำระเงินต้น) - ยอดชำระต่อเดือน
if($payMonth == $i){
$principal = ($calInterest + $payPrincipal) - $resultPmt;;
}
echo "<td>".number_format($principal, 2, '.', '')."</td>";
echo "</tr>";
}
?>
</table>
</div>
</body>
</html>
ตัวอย่างการทำงาน
โดยในงวดสุดท้าย จะมีการนำดอกเบี้ยมารวมใน ยอดชำระต่อเดือน ทำให้ยอดหนี้คงเหลือ ในงวดสุดท้าย
เป็น 0 พอดีครับ
ขอขอบคุณข้อมูลจาก https://www.1213.or.th