Amortization calculator in ASP.net using C#


Hello friends,

One day one of my colleague asked my about amortization calculator, I was just wonder how it works I have started asking about some tips and tricks behind that. He explained me and I started looking some basics of that.

I got little bit reference from web thanks to that guy. Here I have created my Amortization Calculator; I hope it is working fine. Please check and rectify it by matching other calculator it is only the demo version might be some flaw inside that.
Check it before you deploy or integrate with your websites.

.aspx code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Calculator.aspx.cs" Inherits="Calculator" %>
DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Calculatortitle>
    <script type="text/javascript" src="amortizationjs.js">script>
head>
<body>
<center>
    <form id="form1" runat="server">
    <div>
    <table>
            <tr><td> Loan Amounttd>
              <td><input id="amount" name="amount" runat="server" size="8" />td>
            tr>
            <tr><td> Interest Ratetd>
              <td><input id="rate" name="rate" runat="server" size="5" />td>
            tr>
            <tr><td> Number of Paymentstd>
              <td><input id="numPay" name="numPay" runat="server" size="5" />td>
            tr>
            <tr><td>td>
              <td>
              <asp:Button ID="btnCal" runat="server" Text="Calculate" onclick="btnCal_Click" /> 
              <input type="reset" value="ReSet" />
              td>
            tr>
          table>
    div>
   
   
   
    <div>
    <asp:GridView ID="gvAmort" runat="server" CellPadding="4" ForeColor="Black" GridLines="None" Width="100%">
        <RowStyle BackColor="Gray" HorizontalAlign="Center" Font-Names="verdana" Font-Size="Small" />
        <FooterStyle BackColor="DarkGray" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="DarkGray" Font-Bold="True" HorizontalAlign="Center" ForeColor="White" />
        <AlternatingRowStyle BackColor="LightGray" />
    asp:GridView>
    div>
   
    form>
    center>
body>
html>

.asps.cs code:

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Text;

public partial class Calculator : System.Web.UI.Page
{
    DataTable tblAmort = new DataTable("Amort");

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

        }
    }
    protected void btnCal_Click(object sender, EventArgs e)
    {

        double decDeductBalance;
        double interestPaid;
        double decNewBalance;
        double dblTotalPayments;
        double dblInterestToDecimal;
        //double dblPrincipal=;
        int intPmt = 1;
        double loanAmount = Convert.ToDouble(amount.Value);             //Loan Amount
        double iRate = Convert.ToDouble(rate.Value);                    //Interest Rate
        int noPayment = Convert.ToInt32(numPay.Value);                  //No. of payment

        double dblMonthlyPayment = calcMonthly(loanAmount, noPayment, iRate);

        //convert interest rate to decimal form
        dblInterestToDecimal = iRate / 100;
        //calculate interest
        double dblConvertInterest = dblInterestToDecimal / 12;

        //calculate the total number of payments (n * 12)
        int intYears = noPayment;     //years
        //int intNumOfPayments = intYears * 12;    //In Years (with Month wise)
        int intNumOfPayments = intYears;  // Only number of installments basis like (2 Installment or 3 Installment)

        dblTotalPayments = intNumOfPayments * dblMonthlyPayment; //total amount
        decNewBalance = loanAmount; //principle amount
        tblAmort.Columns.Add("Sr.", System.Type.GetType("System.String"));
        tblAmort.Columns.Add("Payment", System.Type.GetType("System.String"));
        tblAmort.Columns.Add("Interest", System.Type.GetType("System.String"));
        tblAmort.Columns.Add("Principal", System.Type.GetType("System.String"));
        tblAmort.Columns.Add("Balance", System.Type.GetType("System.String"));

        DataRow tRow;

        while (intPmt <= intNumOfPayments)
        {
            tRow = tblAmort.NewRow();
            interestPaid = decNewBalance * dblConvertInterest;
            decDeductBalance = dblMonthlyPayment - interestPaid;
            decNewBalance = decNewBalance - decDeductBalance;

            tblAmort.Rows.Add(tRow);
            tRow["Sr."] = intPmt.ToString();
            tRow["Payment"] = String.Format("{0:n2}", dblMonthlyPayment);
            tRow["Interest"] = String.Format("{0:n2}", interestPaid);
            tRow["Principal"] = String.Format("{0:n2}", decDeductBalance);
            tRow["Balance"] = String.Format("{0:n2}", decNewBalance);
            intPmt += 1;
        }
        gvAmort.DataSource = tblAmort;
        gvAmort.DataBind();

    }

    protected double calcMonthly(double principalAmt, double noOfPayment,double iRate)
    {
        double monthly;
        double intRate = (iRate / 100) / 12;
        monthly = (principalAmt * (Math.Pow((1 + intRate), noOfPayment)) * intRate / (Math.Pow((1 + intRate), noOfPayment) - 1));
        return Convert.ToDouble(monthly);
    }
}

Comments

Gman said…
Hi

I pasted this into a new C# project in Visual Studio and it will complain when trying to run, also design tab shows no HTML render.

I think when I copy paste the code its missing some code or ending brackets such as "" etc
Gman said…
Also missing amortizationjs.js
VedPathak said…
Hi Gman,

Sorry to delayed reply. Actually there is no use of .js file It's all in .cs. You can try and send me the error what you are getting. copy and paste the proper tag it might be not proper in your .aspx page. check it by copying in text editor and then to .aspx page.
Realmoney said…
This truly informative stuff! Thanks for taking me to
amortization calculator
Vivek Gupta said…
Thank you very much, It was the thing i was looking for.
VedPathak said…
Thank you guys for your appreciation..
Unknown said…
Thank you..it's working fine.
VedPathak said…
Thank you Subhankar Maiti..
Anonymous said…
Good Blog.Credit scores are the carte de visite for both, the small businesses or for the well-established businesses. Getting a proper loan for a startup or a new venture is a task in itself. And, if it is accompanied by a poor credit history or low credit scores, then the task becomes even more difficult. Most banks do not prefer to provide start up business loans for new ventures. So, it becomes a herculean task to get bad credit business loans from them.Is having a poor credit score stopping you from starting your own business? Avail business loans
From  Mr Pedro, Mr Pedro is a loan officer who works with a USA loan company and they offer international loans at 2% annual return. Here is Mr Pedro email contact  pedroloanss@gmail.com     and start your new venture.