title: Loan Amortization Code Calculation
body: <p>I have a loan amortization code that I found online and made some modifications to suit my needs (C#). The problem that I have is with the calculation of the monthly interest which is enclosed in a loop. It does not return the correct values.</p>
  
  <p>Below is my code (a little long):</p>
  
  <pre><code> 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&lt;AmortPayment&gt; amortPaymentList = new List&lt;AmortPayment&gt;();
  
              // Loop for amortization term (number of monthly payments)
              for (int j = 0; j &lt; amortizationTerm; j++)
              {
                  // Calculate monthly cycle
                  monthlyInterest = currentBalance * interestRate;  **&lt;-----problem here**
                  monthlyPrincipal = monthlyPayment - monthlyInterest;
                  currentBalance = currentBalance - monthlyPrincipal;
  
                  if (j == amortizationTerm - 1 &amp;&amp; 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);
              }
          }
  </code></pre>
  
  <p>Can someone please tell me where I am going wrong with the statement  <code>monthlyInterest = currentBalance * interestRate;</code>?
  Any help appreciated.</p>