|
-
Mar 24th, 2013, 12:57 PM
#1
Thread Starter
Junior Member
Getting Balance from selections made in program (Visual Basic)
Hello, I am trying to get the balance from a program that I wrote, but seem to be having a little trouble with it. The formula for getting balance in this program is (balance = balance * (1 + interestRate). The current year is 2013, but the balance should be calculated for your selected year (e.g., 2015, 2016, 2017). I think repetition should be used, but not entirely sure of how to do that.
Code:
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, balance, initialBalanceSavings, initialBalanceCorporate, finalBalance As Double
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked And radButton2015.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
interestRate = 0.12
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
finalBalance = initialBalanceSavings + initialBalanceCorporate
balance = finalBalance * (1 + interestRate)
txtBoxEstimatedBudget.Text = balance
End Sub
This is what I have so far, its getting me a balance (e.g. if I pick 7 % interest rate and put in 2000 as my initial balance it will give me 2140 as my estimated budget in the text box, but I also had the 2015 checkbox checked, so it is giving me the balance for 2013 I believe, not 2015).
Thanks for any help!!
-
Mar 24th, 2013, 02:05 PM
#2
Re: Getting Balance from selections made in program (Visual Basic)
I think repetition should be used
Why? There are formulas for the calculation of both simple and compound interest. Look 'em up. It's what Google's for. (There other search engines available apparently!)
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 24th, 2013, 02:08 PM
#3
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
Would something like i= p * r * t work? (principal, rate, time)
-
Mar 24th, 2013, 02:12 PM
#4
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
Could you give me a brief example of how to implement that into my code?
-
Mar 24th, 2013, 02:15 PM
#5
Re: Getting Balance from selections made in program (Visual Basic)
I reckon, if it's simple interest (it's not exactly clear from the initial formula you were given). The balance would simply be i + p.
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 24th, 2013, 02:16 PM
#6
Re: Getting Balance from selections made in program (Visual Basic)
 Originally Posted by jmsutton334
Could you give me a brief example of how to implement that into my code?
Er ...
i = p * r * t
Balance = i + p
As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"
Reviews: "dunfiddlin likes his DataTables" - jmcilhinney
Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!
-
Mar 24th, 2013, 02:18 PM
#7
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
And would t = the choice they make in year (e.g. 2015, 2016, 2017)?
-
Mar 24th, 2013, 02:33 PM
#8
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
Code:
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, time, i, balance As Double
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
interestRate = 0.12
End If
If radButton2015.Checked Then
time = 2
End If
If radButton2016.Checked Then
time = 3
End If
If radButton2017.Checked Then
time = 4
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
This is what I got, I still feel like it isn't giving me the right number though... What do you think?
-
Mar 24th, 2013, 02:40 PM
#9
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
I think it needs to be in a loop, but im not sure of how to write that...
-
Mar 24th, 2013, 02:47 PM
#10
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, time, i, balance As Double
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
interestRate = 0.12
End If
If radButton2015.Checked Then
time = 2
End If
If radButton2016.Checked Then
time = 3
End If
If radButton2017.Checked Then
time = 4
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
finalBalance = initialBalanceSavings + initialBalanceCorporate
i = finalBalance * interestRate * time
balance = i + finalBalance
txtBoxEstimatedBudget.Text = balance
End Sub
Sorry this is the code I have now, not the one above
-
Mar 25th, 2013, 05:51 AM
#11
Re: Getting Balance from selections made in program (Visual Basic)
Couple of points. Firstly, use Decimal for any monetary value, not Double. Doubles cannot exactly represent decimal quantities and with repeated calculations will introduce a small error. You should reserve them for continuous values, and for all discrete values (such as money) use Decimal instead.
Secondly, you're probably looking at Compound Interest here. Calculate the interest for the year, round to pennies, and then add to the balance. Use that incremented balance as the input to the next year's interest calculation. I believe there is a formula for compound interest after n years, but since you need to round after each step, I'm not sure that'd work.
-
Mar 25th, 2013, 05:58 AM
#12
Re: Getting Balance from selections made in program (Visual Basic)
Here's an example of the error introduced when adding doubles: Adding 0.1 to itself 1000 times should result in 100, right?
vbnet Code:
Module Module1
Sub Main()
Dim decimalCounter As Decimal = 0
Dim doubleCounter As Double = 0
For additionCounter = 1 To 1000
decimalCounter += 0.1D
doubleCounter += 0.1
Next
Console.WriteLine("Decimal: {0}; Double: {1}", decimalCounter, doubleCounter)
Console.ReadLine()
End Sub
End Module
Result:
Code:
Decimal: 100.0; Double: 99.9999999999986
-
Mar 25th, 2013, 11:36 AM
#13
Thread Starter
Junior Member
Re: Getting Balance from selections made in program (Visual Basic)
This is my code I added a for loop but am still getting the wrong answer. I added a table of what the balances should be with the inputted numbers of (2000, Savings and 1000, Corporate)
Any help would be amazing!
I am getting $2805.10 for (Savings account 7%) for each year(2015-2017) that the user picks. I should be getting a different answer for each year....
Year Savings Account Corporate Account Saving + Corporate
2013 $2000.00 $1000.00 $3000.00
2014 $2140.00 $1050.00 $3190.00
2015 $2289.8 $1102.5 $3392.3
2016 $2450.086 $1157.625 $3607.711
2017 $2621.59202 $1215.50625 $3837.09827
Public Class Form1
Private Sub btnMediaEstimatedFund_Click(sender As Object, e As EventArgs) Handles btnMediaEstimatedFund.Click
Dim interestRate, initialBalanceSavings, initialBalanceCorporate, finalBalance, theYear, balance, subTotal As Double
txtBoxEstimatedBudget.Enabled = False
txtBoxAgenciesNeeded.Enabled = False
If radButtonTraditional.Checked Then
txtBoxAgenciesNeeded.Text = 3
ElseIf radButtonEMedia.Checked Then
txtBoxAgenciesNeeded.Text = 2
End If
If checkBoxSavings.Checked Then
interestRate = 0.07
ElseIf checkBoxCorporate.Checked Then
interestRate = 0.05
ElseIf checkBoxCorporate.Checked And checkBoxSavings.Checked Then
interestRate = 0.12
End If
If radButton2015.Checked Then
theYear = 2015
End If
If radButton2016.Checked Then
theYear = 2016
End If
If radButton2017.Checked Then
theYear = 2017
End If
Dim inputtedData As String
If checkBoxSavings.Checked Then
Do
inputtedData = InputBox("Please enter a balance for SAVINGS account between $500.00 and $3000.00", "Initial Savings Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to cancel calculation!")
Exit Sub
Else
initialBalanceSavings = CType(inputtedData, Single)
If initialBalanceSavings > 3000 Or initialBalanceSavings < 500 Then MsgBox("Please enter a balance for SAVINGS account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceSavings >= 500 And initialBalanceSavings <= 3000
End If
If checkBoxCorporate.Checked Then
Do
inputtedData = InputBox("Please enter a balance for CORPORATE account between $500.00 and $3000.00", "Initial Corporate Balance", "0.00")
If inputtedData = "" Then
MsgBox("User chose to Cancel calculation!")
Exit Sub
Else
initialBalanceCorporate = CType(inputtedData, Single)
If initialBalanceCorporate > 3000 Or initialBalanceCorporate < 500 Then MsgBox("Please enter a balance for CORPORATE account equal to or above $500.00 and no more than $3000.00", MsgBoxStyle.Critical, "Error")
End If
Loop Until initialBalanceCorporate >= 500 And initialBalanceCorporate <= 3000
End If
finalBalance = initialBalanceSavings + initialBalanceCorporate
For theYear = 2013 To 2017
subTotal = finalBalance * (1 + interestRate)
finalBalance = subTotal
Next
txtBoxEstimatedBudget.Text = FormatCurrency(finalBalance)
End Sub
-
Mar 25th, 2013, 04:24 PM
#14
Re: Getting Balance from selections made in program (Visual Basic)
 Originally Posted by jmsutton334
I should be getting a different answer for each year....
Code:
If radButton2015.Checked Then
theYear = 2015
End If
If radButton2016.Checked Then
theYear = 2016
End If
If radButton2017.Checked Then
theYear = 2017
End If
Code:
For theYear = 2013 To 2017
subTotal = finalBalance * (1 + interestRate)
finalBalance = subTotal
Next
When you look at those two bits of code next to each other, can you see the problem?
-
Mar 25th, 2013, 04:26 PM
#15
Re: Getting Balance from selections made in program (Visual Basic)
Also, you don't get fractions of a penny.
Code:
Year Savings Account Corporate Account Saving + Corporate
2013 $2000.00 $1000.00 $3000.00
2014 $2140.00 $1050.00 $3190.00
2015 $2289.8 $1102.5 $3392.3
2016 $2450.086 $1157.625 $3607.711 <---- These figures would be rounded
2017 $2621.59202 $1215.50625 $3837.09827 <----- which will affect these calculations, which also would need to be rounded
-
Mar 25th, 2013, 04:48 PM
#16
New Member
Re: Getting Balance from selections made in program (Visual Basic)
Do a search for: Time Value of Money. FV = PV*(1+i)^n where n = number of periods
Try this: http://en.wikipedia.org/wiki/Time_va..._a_present_sum
Hope this helps.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|