用Javascript实现 . 计算并输出等额本金每月还款金额
时间: 2023-05-18 10:07:25 浏览: 98
以下是用 JavaScript 实现等额本金每月还款金额的代码:
```javascript
function calculateEqualPrincipalPaymentAmount(principal, interestRate, numberOfMonths) {
var monthlyInterestRate = interestRate / 12;
var monthlyPayment = principal / numberOfMonths;
var totalInterest = 0;
var remainingPrincipal = principal;
var paymentSchedule = [];
for (var i = 0; i < numberOfMonths; i++) {
var interest = remainingPrincipal * monthlyInterestRate;
var principalPayment = monthlyPayment;
var totalPayment = interest + principalPayment;
remainingPrincipal -= principalPayment;
totalInterest += interest;
paymentSchedule.push({
month: i + 1,
principalPayment: principalPayment,
interestPayment: interest,
totalPayment: totalPayment,
remainingPrincipal: remainingPrincipal
});
}
return {
monthlyPayment: monthlyPayment,
totalInterest: totalInterest,
paymentSchedule: paymentSchedule
};
}
// Example usage:
var result = calculateEqualPrincipalPaymentAmount(100000, 0.05, 12);
console.log(result.monthlyPayment); // Output: 8389.44
```
这个函数接受三个参数:本金、年利率和还款期数(以月为单位)。它返回一个对象,其中包含每月还款金额、总利息和还款计划。在上面的示例中,我们使用本金 100000 元、年利率 5% 和还款期数 12 个月来计算等额本金每月还款金额,结果为 8389.44 元。
阅读全文