2013-02-08 94 views
1

我有一個貸款攤銷代碼,我在網上找到並做了一些修改以滿足我的需求(C#)。我的問題是計算循環中包含的每月利息。它不會返回正確的值。貸款攤銷代碼計算

下面是我的代碼(有點長):

private void CalculateLoan() 
     { 
      // Make sure we use types that hold decimal places    
      DateTime payDate = DateTime.ParseExact(_startDate.Value, "yyyyMMdd", null); 
      double interestRate = 0; 
      double monthlyInterest = 0; 
      double loanAmount; 
      short amortizationTerm = 0; 
      double currentBalance; 
      double cummulativeInterest = 0; 
      double monthlyPrincipal = 0; 
      double cummulativePrincipal = 0; 

      loanAmount = double.Parse(_principal.Value); 
      currentBalance = loanAmount; 
      interestRate = double.Parse(_interestRate.Value) * 0.01; 
      amortizationTerm = short.Parse(_period.Value); 

      // Calculate the monthly payment and round it to 2 decimal places   
      var monthlyPayment = ((interestRate/12)/(1 - (Math.Pow((1 + (interestRate/12)), -(amortizationTerm))))) * loanAmount; 
      monthlyPayment = Math.Round(monthlyPayment, 2); 

      // Storage List 
      List<AmortPayment> amortPaymentList = new List<AmortPayment>(); 

      // Loop for amortization term (number of monthly payments) 
      for (int j = 0; j < amortizationTerm; j++) 
      { 
       // Calculate monthly cycle 
       monthlyInterest = currentBalance * interestRate; **<-----problem here** 
       monthlyPrincipal = monthlyPayment - monthlyInterest; 
       currentBalance = currentBalance - monthlyPrincipal; 

       if (j == amortizationTerm - 1 && currentBalance != monthlyPayment) 
       { 
        // Adjust the last payment to make sure the final balance is 0 
        monthlyPayment += currentBalance; 
        currentBalance = 0; 
       } 

       // Reset Date 
       payDate = payDate.AddMonths(1); 
       // Add to cummulative totals 
       cummulativeInterest += monthlyInterest; 
       cummulativePrincipal += monthlyPrincipal; 

       amortPaymentList.Add 
        (new AmortPayment 
        { 
         RowNumber = j + 1, 
         Date = payDate, 
         ScheduledPayment = Math.Round(monthlyPayment, 2),           
         Interest = Math.Round(monthlyInterest, 2), 
         TotalRepayment = Math.Round(monthlyPayment + monthlyInterest, 2), 
         Balance = Math.Round(currentBalance, 2), 
         TotalInterest = Math.Round(cummulativeInterest, 2), 
         TotalBalance = Math.Round(currentBalance + cummulativeInterest, 2) 
        }); 

       // Add values to SAP matrix   
       _rowNo.Value = (j + 1).ToString(); 
       _date.ValueEx = payDate.ToString("yyyyMMdd"); 
       _payment.Value = monthlyPayment.ToString(); 
       _interest.Value = monthlyInterest.ToString(); 
       _totalRepayment.Value = (monthlyPayment + monthlyInterest).ToString(); 
       _balancePrincipal.Value = currentBalance.ToString(); 
       _balanceInterest.Value = cummulativeInterest.ToString(); 
       _total.Value = (currentBalance + cummulativeInterest).ToString(); 

       _form.Freeze(true); 
       oMatrix.AddRow(); 
       _form.Update(); 
       _form.Freeze(false); 
      } 
     } 

是否有人可以告訴我在哪裏,我的發言monthlyInterest = currentBalance * interestRate;回事? 任何幫助表示讚賞。

回答

1

您應該使用mountly利率,而不是每年,這樣做只是devide利率12

monthlyInterest = currentBalance * interestRate/12; 
+0

啊,非常感謝維亞切斯拉夫·Smityukh。沒有注意到這一點。 –

+0

你的答案是正確的,但我們如何計算季度。 – user3839710