0

I'm a first year programming student and am trying to make a mortgage payment calculator. Users enter their loan amount and interest rate, select 15 or 30 years for the term length, and their monthly payment should be calculated. I think the math is correct, but I think I'm running into problems with converting data types. The only result I'm able to calculate is "Infinity". I've also tried declaring all variables as Doubles, but no difference.

Below is the piece of code I'm having trouble with, and Option Strict On is included in the full code. Any tips would be greatly appreciated!

    Dim decLoanAmount As Decimal
    Dim decInterestRate As Decimal
    Dim decFifteen As Decimal = 180D
    Dim decThirty As Decimal = 360D
    Dim decNumberOfMonths As Decimal
    Dim dblPayment As Double
    Dim decPayment As Decimal

    ' Did user enter a numeric value?

    If IsNumeric(txtLoanAmount.Text) And IsNumeric(txtInterestRate.Text) Then
        decLoanAmount = Convert.ToDecimal(txtLoanAmount.Text)
        decInterestRate = Convert.ToDecimal(txtInterestRate.Text)

        ' Is Loan Amount greater than zero?
        If decLoanAmount > 0 And decInterestRate > 0 Then
            If radFifteen.Checked = True Then
                decNumberOfMonths = decFifteen
            ElseIf radThirty.Checked = True Then
                decNumberOfMonths = decThirty
            End If

            ' Calculate the monthly payments as a double
            dblPayment = (decLoanAmount * (decInterestRate / 12 / 100) * (1 + (decInterestRate / 12 / 100) _
                ^ decNumberOfMonths)) / ((1 + (decInterestRate / 12 / 100) ^ decNumberOfMonths) - 1)
            ' Convert double to decimal
            decPayment = Convert.ToDecimal(decPayment)
            ' Display monthly payment
            lblPayment.Text = decPayment.ToString("C2")

        Else
            If decLoanAmount < 0 Then
                MsgBox("Please enter a valid loan amount.", , "Input error")
            End If

            If decInterestRate < 0 Then
                MsgBox("Please enter a valid interest rate.", , "Input error")
            End If

        End If

    Else
        ' Display error message if user entered a negative value.
        MsgBox("Please enter a numeric value.", , "Input Error")
    End If
End Sub
Tim Williams
  • 154,628
  • 8
  • 97
  • 125
SJW
  • 1
  • 1
    I'd suggest factoring it out a bit, and checking the values are as you'd expect - for example, `(decInterestRate / 12 / 100)` is repeated in the calculation, so why not calculate that up front, and check the value is as you'd expect. – Rowland Shaw Dec 07 '14 at 21:08
  • 1
    Also, `decPayment = Convert.ToDecimal(decPayment)` isn't going to do a whole lot... – Rowland Shaw Dec 07 '14 at 21:09
  • 1
    You are not expecting people here to write and/or debug code for you, right? Show us some research, provide sample input, output, explain a bit more. Assume the code above wasn't there, do you think what you described is enough to help you? – Victor Zakharov Dec 07 '14 at 21:48
  • 1
    did you perhaps mean `decPayment = Convert.ToDecimal(dblPayment)`? – Ňɏssa Pøngjǣrdenlarp Dec 07 '14 at 21:58

1 Answers1

0

Based on the Wikipedia formula, you aren't doing it correctly. It should look something like this:

dblPayment = (decLoanAmount * (decInterestRate / (12 * 100))) / (1 - (1 + (decInterestRate / (12 * 100))) ^ (-decNumberOfMonths))

Also, this line is weird

decPayment = Convert.ToDecimal(decPayment)

Should be

decPayment = Convert.ToDecimal(dblPayment)
the_lotus
  • 12,668
  • 3
  • 36
  • 53