# Solved: Mortgage Calculator - Visual Basic



## bravo4583 (Oct 4, 2012)

I am trying to calculate monthly payment for a loan. Principle amount and interest is entered by user and term defaults to 30 years. For the payment calculation, I have tried different ways of setting up the formula but I keep getting "NaN" as a result. I know this means "Not a Number" but I'm not sure how I'm getting that.

Yes, this is a school assignment but it's an online course and every time I email the instructor he takes about a week to reply back. Once he does reply he says to talk to my classmates (who don't know either!)

You can see my frustration. Would someone please look over the code that I have and tell me what is wrong and why I keep getting NaN as a result? Thanks...FYI, my calculations show 3 different payment formulas I have tried (I figured one of those would do the trick but no luck yet). They have been commented out, it's hard to see that without the color coding from VB.



' Module: frmMortgage.vb

Option Explicit On

Public Class frmMortgage

Dim gsngPrincipal As Double
Dim gsngInterestRate As Double
Dim gsngPayment As Double

Private Sub btnCalculate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCalculate.Click
'
'Retrieve principal and interest rate amounts from form's text boxes
'Use Val function to convert text to values
'Divide interest rate by 1200 to get monthly interest rate
'
gsngPrincipal = Val(Me.txtAmount.Text)
gsngInterestRate = Val(Me.txtRate.Text) / 1200.0
'
'If either amount is 0 or less, display error message,
'clear input text boxes, and exit procedure to start over.
'
If gsngPrincipal <= 0.0 Then
MsgBox("Principal amount value is invalid.", 0, "Input Error!")
Call btnClear_Click(sender, e)
Exit Sub
ElseIf gsngInterestRate <= 0.0 Then
MsgBox("Interest rate value is invalid.", 0, "Input Error!")
Call btnClear_Click(sender, e)
Exit Sub
Else
'
'Calculate payment and display it
'
gsngPayment = payment()
Me.txtPayment.Text = Format(gsngPayment, "#####.##")
End If
End Sub

Private Sub btnClear_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClear.Click
'
'Clear all text boxes and set focus to Amount text box
'
txtAmount.Clear()
txtPayment.Clear()
txtRate.Clear()
txtAmount.Focus()
End Sub

Private Sub btnExit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnExit.Click
'
' Exit the program
'
Me.Close()
End Sub

End Class


'Module name: basGlobal.vb
'
Public Const gintYears As Integer = 30
Dim gsngPrincipal As Double
Dim gsngInterestRate As Double
Dim gsngPayment As Double
'Dim monthlyPayment As Double
'Dim power As Double = (gsngInterestRate + 1)

Sub main()
Application.Run(frmMortgage)
End Sub

Public Function payment() As Double

'Returns monthly payment on mortgage or other loan
'Loan term defined by Public Constant gintYears
'
'gsngPrincipal = loan principal
'gsngInterestRate = monthly interest rate

Return gsngPrincipal * (gsngInterestRate / (1.0 - (gsngInterestRate + 1) ^ -gintYears))

'Return gsngPrincipal * (gsngInterestRate / 1.0 - (Math.Pow(power, -gintYears)))

'monthlyPayment = (gsngPrincipal * gsngInterestRate) / (1 - (1 + gsngInterestRate) ^ (-gintYears))

'Return monthlyPayment

End Function

End Module


----------



## cpscdave (Feb 25, 2004)

In the future put CODE tags around your code 

```
It will help
     by perserving the formatting of the code 
     making it easier to read :)
```
Nothing leaps out imediatly, and I don't have to tools here to run the code and debug it. 
But heres a couple of possiblities

FIRST you have gintYears as a integer where the other 2 are doubles. This sometimes causes problems. Add casting or try making it a double as well. Often a double * int = WHO KNOWS  Based on what you're trying to do you'll likely have to break up the formula into multiple steps and cast along the way. (for debugging purposes this is handy as you can output after each step and see whats going on)

The second possiblity is you're trying to do math with a non-number type. Likely a string so check your varibles before doing the calc

Hope that helps


----------



## bravo4583 (Oct 4, 2012)

Thank's for your help. I figured it out. I didn't change gintYears from integer to double but I did check my formula over and over until I realized that the formula calls for the term in 'months', not 'years'. The fix was as simple as multiplying gintYears by 12 and it works perfectly. 

I do have a question though. How do I use CODE TAGS for future reference? I've also seen in other posts that they have line numbers which helps in referencing lines of code as well. How can I do that here?


----------



## cpscdave (Feb 25, 2004)

To use code tags make sure you're in the "advanced" editor and then its the icon that looks like a #

Or type [CDE] paste your code here [/CDE]

just spell CODE correctly


----------



## bravo4583 (Oct 4, 2012)

```
test...test...code goes here ->
```


----------



## bravo4583 (Oct 4, 2012)

Thanks cpscdave. Is there also a way to include line numbers?


----------

