Mortgage calculator remaining balance miscalculation

1.6k views Asked by At

I am writing a mortgage calculator that will populate a DataGridView all the payments and how much interest and principal for each payment. Then the last column is the remaining balance after the payment.

Here is the problem:

The Mortgage payment, interest and principal calculations are all good, but for some reason the remaining balance calculation seems wrong because at the end of the payment period there is still a large balance.

My code may be sloppy, because I am still new to vb. In fact it's my first class.


Private Sub AmortButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AmortButton.Click 'initiate click event'
    Dim Amt As Double = Amount.Text() 'read in amount'
    Dim Intr As Double = Intrest.Text() 'read in intrest'
    Dim Yrs As Double = Term.Text() 'read in years'
    Dim payment As Double = (Yrs * 12) 'convert years to payment periods
    '
    Dim IntrDec As Double = ((Intr / 100) / 12) 'convert APR to monthly rate as a decimal'
    Dim TempAmt As Double = Amt 'setting Temperory balance
    For i As Double = 1 To payment Step i + 1 'setting for-loop paramaters'
        Dim MP = Math.Round((Pmt(IntrDec, payment, Amt) * -1), 2) 'calculate Mortgage payment'
        Dim IP = Math.Round((IPmt(IntrDec, i, payment, Amt) * -1), 2) 'calculate Intrest paid'
        Dim PP = Math.Round((PPmt(IntrDec, i, payment, Amt) * -1), 2) 'calculate priciple paid
        Amt = Amt - PP 'setting new balance'
        Dim RM = Math.Round((Amt), 2) 'rounding balance to two decimals'
        AmortTable.Rows.Add(i, MP, IP, PP, RM) 'adding row entry to table'
    Next 'end for-loop'

End Sub

2

There are 2 answers

6
Jim O'Neil On BEST ANSWER

You have a logic error in your loop. You are changing the amount of the loan every time the loop runs

Amt = Amt - PP 'setting new balance'

I notice you have TempAmt declared above the loop, so it looks like you anticipated needing it at some point :); decrement that instead of Amt each time and assign to RM

Also, the STEP i+1 isn't necessary, and while it works here, it's not strictly correct. It will set the loop iteration to whatever the value of i is PLUS 1 when the loop is initialized. Here you're lucky because i defaulted to zero at the beginning. Bottom line, the STEP should be a constant, and since the default is the value 1 you don't need it at all here.

And the advice by paulsm4 is spot on as well, but in a sample like you have you'd likely see small aberrations as a result - a few pennies or so at the end for instance. Floating point arithmetic is undoubtedly a topic you'll get into in a future class.

3
paulsm4 On

Nothing to do with VB.Net per se.

The problem is that floating point numbers are an "approximation" - you can sometimes encounter "surprising" results when you use them. Similarly, you can also get "surprises" when you mix floating point (e.g. "Intr") and integer (e.g. "Intr / 100").

SUGGESTION:

Try changing "i" and "payment" to "integer", and see if you get any different results.

ALSO:

These links might help: