|
||||||
A Simple VBScript Home Mortgage CalculatorCalculating Compound Interest Installments with a Windows ComputerThe 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:
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 first step is, therefore, to create a function that will calculate the monthly payment. A VBScript Mortgage Calculator FunctionThe key function for a VBScript mortgage calculator will return the monthly amount expected. It will have 3 inputs:
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 InterfaceThe user interface will:
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.
The copyright of the article A Simple VBScript Home Mortgage Calculator in Windows Programming is owned by Mark Alexander Bain. Permission to republish A Simple VBScript Home Mortgage Calculator in print or online must be granted by the author in writing.
|
||||||
|
|
||||||
|
|
||||||