Code to calculate monthly payment VB.net

I need to calculate my monthly payment given the loan principal, the percentage, and the terms.  Here are my variables.

      dblPrincipal = CDbl(txtPrincipal.Text)
        dblRate = CDbl(txtRate.Text)
        dblYears = CDbl(txtYears.Text)
gmahler5thAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

flavoCommented:
Use this :

dblMonth = dblYears * 12
dblRateMonth = dblRate / 12

dblRate = dblPrincipal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)

That Finance Maths subject finally paid off :-)

Dave

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
flavoCommented:
woops

dblPayment  = dblPrincipal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)
0
RonaldBiemansCommented:
There is a function in .net that will calculate that for you, the PMT function. Like

Sub TestPMT()
        Dim PVal, APR, FVal, Payment, TotPmts As Double
        Dim PayType As DueDate
        Dim Fmt As String
        Dim Response As MsgBoxResult
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.
        PVal = CDbl(InputBox("How much do you want to borrow?"))
        APR = CDbl(InputBox("What is the annual percentage rate of your loan?"))
        If APR > 1 Then APR = APR / 100 ' Ensure proper form.
        TotPmts = CDbl(InputBox("How many monthly payments will you make?"))
        Response = MsgBox("Do you make payments at the end of month?", MsgBoxStyle.YesNo)
        If Response = MsgBoxResult.No Then
            PayType = DueDate.BegOfPeriod
        Else
            PayType = DueDate.BegOfPeriod
        End If
        Payment = Pmt(APR / 12, TotPmts, -PVal, FVal, PayType)
        MsgBox("Your payment will be " & Format(Payment, Fmt) & " per month.")
    End Sub


0
flavoCommented:
maybe i did spend 6 months for nothing.... :-)
0
gmahler5thAuthor Commented:
Ronald, Thank you for sharing.  I like the ease and flexibility of this function.  I sort of hacked it up and put it in my own subroutine, and I am coming up with a monthly payment in the 10s of thousands. Can you look to see that I have used this function correctly?

 'declare your variables here
    Dim dblPrincipal, dblMonth, dblRateMonth, dblRate, dblYears, dblAmount As Double
    Dim PVal, APR, FVal, Payment, TotPmts As Double
    Dim Fmt As String

    Private Sub btnCalculate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCalculate.Click

        'assign the values from textboxes to the variables
        PVal = CDbl(txtPrincipal.Text)
        APR = CDbl(txtRate.Text)
        TotPmts = CDbl(txtYears.Text)
        dblMonth = dblYears * 12
        dblRateMonth = dblRate / 12

        'calculate the final amount as principal + interest
        Fmt = "###,###,##0.00"   ' Define money format.
        FVal = 0   ' Usually 0 for a loan.
        Payment = Pmt(APR / 12, TotPmts, -PVal, FVal)

        'put the calculated amount in Payment Amount textbox
        txtAmount.Text = Format(Payment, Fmt)
    End Sub
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.