Code to calculate monthly payment VB.net

Posted on 2004-11-09
Medium Priority
Last Modified: 2008-03-17
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)
Question by:gmahler5th
  • 3
LVL 34

Accepted Solution

flavo earned 1000 total points
ID: 12541367
Use this :

dblMonth = dblYears * 12
dblRateMonth = dblRate / 12

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

That Finance Maths subject finally paid off :-)


LVL 34

Expert Comment

ID: 12541369

dblPayment  = dblPrincipal / ((1 - (1 + dblRateMonth) ^ -dblMonth) / dblRateMonth)
LVL 25

Assisted Solution

RonaldBiemans earned 1000 total points
ID: 12542957
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
            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

LVL 34

Expert Comment

ID: 12542983
maybe i did spend 6 months for nothing.... :-)

Author Comment

ID: 12551046
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
Suggested Courses
Course of the Month13 days, 20 hours left to enroll

807 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question