|
|
Title | Roughly compare the savings a normal bank account would give versus a 401(k) (a US tax thing) in Visual Basic .NET |
Description | This example shows how to roughly compare the savings a normal bank account would give versus a 401(k) (a US tax thing) in Visual Basic .NET. |
Keywords | bank account, savings, interest, tax, tax deferred, 401(k), VB.NET |
Categories | Algorithms |
|
|
Important Note: I am not a tax or investment professional. I don't even pretend to understand this stuff. This is a very simple tool for playing with numbers (it doesn't even compound continuously) and I don't vouch for its correctness. It should in no way be taken for investment advice. What, are you crazy???
That said...
A 401(k) retirement plan lets you save money tax deferred. As I understand it, that means you don't pay tax on that money before you put it into the plan and any interest the money accrues is not taxed. When you remove money from the plan, you pay taxes on it. There are also penalties if you withdraw money before retirement age (which will probably be around 140 by the time I retire).
Enter the annual contribution you would make to the plan, your income tax rate, the annual interest rate you expect to earn, and the number of years you want to examine. When you click Go, the following code executes.
The code gets your inputs. It then loops through the years calculating balances for a normal bank account and for a 401(k).
For the bank account, the code calculates your interest for the previous year, adds that to your contribution, subtracts taxes from both, and adds the result to the account balance.
For the 401(k), the calculation is the same except the program does not subtract taxes.
|
|
Private Sub btnGo_Click(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles btnGo.Click
Dim annual_contribution As Single = _
Val(txtAnnualContribution.Text.Replace("$", _
"").Replace(",", ""))
Dim tax_rate As Single
tax_rate = Val(txtTaxRate.Text.Replace("%", "")) / 100
' See how big it is.
If tax_rate >= 1 Then
' It's a percentage as in 5%.
tax_rate /= 100
End If
Dim interest_rate As Single
interest_rate = Val(txtInterestRate.Text.Replace("%", _
"")) / 100
' See how big it is.
If interest_rate >= 1 Then
' It's a percentage as in 5%.
interest_rate /= 100
End If
Dim num_years As Integer = Val(txtYears.Text)
Dim balance_bank As Single = 0
Dim balance_401k As Single = 0
lvwResults.Items.Clear()
For i As Integer = 0 To num_years
Dim lv_item As New ListViewItem(i)
lvwResults.Items.Add(lv_item)
lv_item.SubItems.Add(FormatCurrency(balance_bank))
lv_item.SubItems.Add(FormatCurrency(balance_401k))
' Bank balance += interest + contribution, minus
' taxes.
balance_bank = balance_bank + _
(1 - tax_rate) * (balance_bank * interest_rate _
+ annual_contribution)
' 401(k) balance += interest + contribution.
balance_401k = balance_401k + _
(balance_401k * interest_rate + _
annual_contribution)
Next i
End Sub
|
|
Over many years, the difference between a bank account and a 401(k) can be quite large. Also many larger employers match 491(k) contributions at least to an extent, further increasing the difference.
(If you know more about these types of accounts and want to chime in, let me know.)
|
|
|
|
|
|