A Simple VBScript Home Mortgage Calculator

Calculating Compound Interest Installments with a Windows Computer

A Simple VBScript Home Mortgage Calculator - Mark Alexander Bain
A Simple VBScript Home Mortgage Calculator - Mark Alexander Bain
The VBScript programmer can easily add a mortgage calculator to an application. This will calculate the monthly payments due on any compound interest loan

Mortgage repayments are an important element of many peoples' lives. These need to be paid every month for long periods of time (perhaps for 20 or 30 years). However, the fact that a mortgage is an example of a compound interest loan make the monthly repayments rather difficult to calculate, as discussed in A Simple Home Mortgage Calculator. Fortunately, the article also shows that the calculation can be made easier by using:

  • a standard formula for calculating the monthly repayments for a mortgage
  • some simple VBScript code to run the calculation

This means, of course, that the VBScript developer can easily create an application that includes a mortgage calculator that will calculate the monthly repayment for any mortgage given:

  • the initial amount borrowed
  • the interest rate
  • the time for which the mortgage is to run

The first step is, therefore, to create a function that will calculate the monthly payment.

A VBScript Mortgage Calculator Function

The key function for a VBScript mortgage calculator will return the monthly amount expected. It will have 3 inputs:

  • the size of the loan
  • the interest rate for the mortgage
  • the number of years over which the mortgage is to be repaid.

This function is:

Function mp (amount, interest, years)
Dim payments: payments = years * 12 'Total number of months
Dim percentage: percentage = interest / 100 / 12 'Montly interest rate
mp = amount * ( percentage * (1 + percentage) ^ payments ) / ( (1 + percentage) ^ payments - 1)
End Function

This can then be called from the script by using something like:

wscript.echo "$" & round(mp (100000, 5, 25),2)

If the script is run then it will return the value $584.59 (as shown in figure 1). However, the developer will not want their users modifying the script, and so the next stage is to produce a simple user interface.

A VBScript Mortgage Calculator Function User Interface

The user interface will:

  • read lines from the standard input to obtain the inputs for the formula
  • calculate the monthly payments according to the inputs
  • return the result to the user

For example:

Sub doMortgage
'Get the inputs
WSscript.Echo "Mortgage Calculator"
WScript.StdOut.Write "Enter Initial Amount> "
Dim amount: amount = WScript.StdIn.ReadLine
WScript.StdOut.Write "Enter Interest> "
Dim interest: interest = WScript.StdIn.ReadLine
WScript.StdOut.Write "Enter repayment time (in years)> "
Dim years: years = WScript.StdIn.ReadLine
'Calculate the monthly rate
Dim monthly: monthly = mp (amount, interest, years)
'Format and display the result
WScript.StdOut.Write "The montly payment for "
WScript.StdOut.Write "$" & amount & " "
WScript.StdOut.Write " for " & years & " "
WScript.StdOut.Write "at " & interest & "% "
WScript.Echo "will be $" & monthly
End Sub

This will not run by default, and so the following will need to be added to the script:

doMortgage

The application user can then use the cscript command to run the application from the Windows prompt (as shown in figure 2), and the programmer can now pass their mortgage calculator on to anyone with a Windows computer.

Mark Alexander Bain - Mark Alexander Bain is a writer, Mo Bro and consultant for all aspects of software development at dsquared. He has also written regularly ...

rss
Advertisement
Advertisement
Advertisement