|
|
Title | Calculate the future value of a monthly investment with interest in Visual Basic .NET |
Description | This example shows how to calculate the future value of a monthly investment with interest in Visual Basic .NET. |
Keywords | algorithms, mathematics, finance, interest, monthly investment, investment, example, example program, Windows Forms programming, Visual Basic .NET, VB.NET |
Categories | Algorithms |
|
|
The magic of compound interest is that, over time, you get interest on the interest
For each month the program calculates the interest on the account balance. It then adds the interest and a monthly contribution to the balance and displays the result. (Note that interest is compounded monthly not continuously.)
The following code shows how the program performs its calculations.
|
|
' Calculate the interest compounded monthly.
Private Sub btnGo_Click(ByVal sender As System.Object, ByVal _
e As System.EventArgs) Handles btnGo.Click
' Get the parameters.
Dim monthly_contribution As Decimal = Decimal.Parse( _
txtMonthlyContribution.Text, NumberStyles.Any)
Dim num_months As Integer = _
Integer.Parse(txtNumMonths.Text)
Dim interest_rate As Decimal = Decimal.Parse( _
txtInterestRate.Text.Replace("%", "")) / 100
interest_rate /= 12
' Calculate.
lvwBalance.Items.Clear()
Dim balance As Decimal = 0
For i As Integer = 1 To num_months
' Display the month.
Dim new_item As ListViewItem = _
lvwBalance.Items.Add(i.ToString())
' Display the interest.
Dim interest As Decimal = balance * interest_rate
new_item.SubItems.Add(interest.ToString("c"))
' Add the contribution.
balance += monthly_contribution
' Display the balance.
balance += interest
new_item.SubItems.Add(balance.ToString("c"))
Next i
' Scroll to the last entry.
lvwBalance.Items(lvwBalance.Items.Count - _
1).EnsureVisible()
End Sub
|
|
|
|
|
|
|
|
|