JavaScript中的年金摊销表

时间:2017-08-07 11:43:12

标签: javascript csv amortization annuity

我需要一个能够计算抵押年金摊销表的功能,其中包括按照以下链接中的每个月排序的cols和行的支付金额,本金支付,利息支付等的输出: Output example

输出文件格式应为.cvs。目前我被困在这个女巫身上还远离结果:



var i = 5/100;
var loanAmount = 15000;
var m = 12;

var monthlyPayment = loanAmount*(i/12)*Math.pow((1+i/12), m) / (Math.pow((1+i/12), m)-1)

var currentBalance = loanAmount;   
var paymentCounter = 1;
var totalInterest = 0;
monthlyPayment = monthlyPayment;
		
while(currentBalance > 0) {
    //this calculates the portion of your monthly payment that goes towards interest
    towardsInterest = (i/12)*currentBalance;  
		
    if (monthlyPayment > currentBalance){
        monthlyPayment = currentBalance + towardsInterest;
    }
		
    towardsBalance = monthlyPayment - towardsInterest;
    totalInterest = totalInterest + towardsInterest;
    currentBalance = currentBalance - towardsBalance;
}




真的很感激任何帮助。

1 个答案:

答案 0 :(得分:0)

尝试这样的事情:

const annuity = (C, i, n) => C * (i/(1-(1+i)**(-n)));

const balance_t = (C, i, P) => {
  const period_movements = {
    base: C
  }
  period_movements.interest = C*i;
  period_movements.amortization = P - (C*i);
  period_movements.annuity = P;
  period_movements.final_value = Math.round((C - period_movements.amortization)*100)/100;

  return period_movements;
}

const display_mortgage = (C, i, n) => {
  const payements = annuity(C, i, n);
  let movements = balance_t(C, i, payements);
  while (movements.final_value>-.01){
    console.log(movements);
    movements = balance_t(movements.final_value, i, payements);
  }
}

display_mortgage(20000, 0.05, 4);

输出:

{ base: 20000,
  interest: 1000,
  amortization: 4640.236652069255,
  annuity: 5640.236652069255,
  final_value: 15360 }
{ base: 15360,
  interest: 768,
  amortization: 4872.236652069255,
  annuity: 5640.236652069255,
  final_value: 10488 }
{ base: 10488,
  interest: 524.4,
  amortization: 5115.836652069255,
  annuity: 5640.236652069255,
  final_value: 5372 }
{ base: 5372,
  interest: 268.6,
  amortization: 5371.6366520692545,
  annuity: 5640.236652069255,
  final_value: 0 }