I'm a first year programming student and am trying to make a mortgage payment calculator. Users enter their loan amount and interest rate, select 15 or 30 years for the term length, and their monthly payment should be calculated. I think the math is correct, but I think I'm running into problems with converting data types. The only result I'm able to calculate is "Infinity". I've also tried declaring all variables as Doubles, but no difference.
Below is the piece of code I'm having trouble with, and Option Strict On is included in the full code. Any tips would be greatly appreciated!
Dim decLoanAmount As Decimal
Dim decInterestRate As Decimal
Dim decFifteen As Decimal = 180D
Dim decThirty As Decimal = 360D
Dim decNumberOfMonths As Decimal
Dim dblPayment As Double
Dim decPayment As Decimal
' Did user enter a numeric value?
If IsNumeric(txtLoanAmount.Text) And IsNumeric(txtInterestRate.Text) Then
decLoanAmount = Convert.ToDecimal(txtLoanAmount.Text)
decInterestRate = Convert.ToDecimal(txtInterestRate.Text)
' Is Loan Amount greater than zero?
If decLoanAmount > 0 And decInterestRate > 0 Then
If radFifteen.Checked = True Then
decNumberOfMonths = decFifteen
ElseIf radThirty.Checked = True Then
decNumberOfMonths = decThirty
End If
' Calculate the monthly payments as a double
dblPayment = (decLoanAmount * (decInterestRate / 12 / 100) * (1 + (decInterestRate / 12 / 100) _
^ decNumberOfMonths)) / ((1 + (decInterestRate / 12 / 100) ^ decNumberOfMonths) - 1)
' Convert double to decimal
decPayment = Convert.ToDecimal(decPayment)
' Display monthly payment
lblPayment.Text = decPayment.ToString("C2")
Else
If decLoanAmount < 0 Then
MsgBox("Please enter a valid loan amount.", , "Input error")
End If
If decInterestRate < 0 Then
MsgBox("Please enter a valid interest rate.", , "Input error")
End If
End If
Else
' Display error message if user entered a negative value.
MsgBox("Please enter a numeric value.", , "Input Error")
End If
End Sub
Based on the Wikipedia formula, you aren't doing it correctly. It should look something like this:
dblPayment = (decLoanAmount * (decInterestRate / (12 * 100))) / (1 - (1 + (decInterestRate / (12 * 100))) ^ (-decNumberOfMonths))
Also, this line is weird
decPayment = Convert.ToDecimal(decPayment)
Should be
decPayment = Convert.ToDecimal(dblPayment)
Related
I am having issues trying to make a calculator that accurately calculates deposits plus the amount of interest that is added monthly. I dont know how to incorporate a loop that can add monthly deposits to total deposits, then adds the total deposits plus total interest times the interest rate / 100 / 12 to the total deposits. Here is what I have so far. Sorry am super new to VB !
Dim intMonthlyDeposit As Interger
Dim intMonthsTotal As Integer
Dim intAnnualRate As Interget
Dim decTotalDeposits As Decimal
Dim decTotalInterest As Decimal
Dim decTotalTotal As Decimal
Try
intMonthlyDeposit = CInt(txtMonthlyDeposits.Text)
intMonthsTotal = CInt(txtMonths.Text)
decAnnualRate = CDec(txtAnnualRate.Text)
decTotalDeposits = 0
decTotalInterest = 0
decTotalTotal = 0
decTotalDeposits = decMonthlyDeposit * intMonthsTotal
decTotalInterest = decTotalDeposits * (decAnnualRate / 100)
decTotalTotal = decTotalDeposits + decTotalInterest
lblTotDeposit.Text = decTotalDeposits.ToString("C")
lblTot.Text = decTotalTotal.ToString("C")
lblTotInterest.Text = decTotalInterest.ToString("C")
Catch ex As Exception
MsgBox("Enter a Valid Number")
End Try
End Sub
This is how I implemented the loop for calculating total interest and total deposits correctly.
For index As Integer = 1 To intMonthsTotal Step 1
decTotalDeposits += decMonthlyDeposit
decTotalInterest += (decTotalDeposits + decTotalInterest) * ((decAnnualRate / 100) / 12)
New to VBA and lousy at it, so please be gentle!
I have the following code which gives the Long type variable "EIa" a value. I was getting a bunch of odd results later down the code so I put in some Debug.Print lines to find my issue and notice that when I Debug.Print the variable EIa, I get 0 but if I Debug.Print EXACTLY the expression that defines EIa, I get the expected value. Code below, any ideas?
'Calculate mA1:
'****************************************************************************************************
EIa = Etimber * ImatA / (Etimber * (ImatA + ImatB + ImatC + ImatD))
Debug.Print "EIa = "; EIa
Debug.Print "EIa = "; Etimber * ImatA / (Etimber * (ImatA + ImatB + ImatC + ImatD))
mA1 = 12 * 0.5 * (q1PSF * EIa) * bMat * LcantiA ^ 2 '12 puts this into lb-in
If LmatSymA = 0 Then
fbA1 = 0
Else
fbA1 = (mA1 * (0.5 * tMatA) / ImatA)
End If
The result of the expression is a kind of fractional, floating point number, but since you defined EIa as Long (a large integer), it gets truncated to zero upon assignment.
See Visual Basic Data Types.
So im pretty close but I continue to get the wrong values. The user is suppose to enter a positive integer and its suppose to add all the integers in between. So if the user enters 5 it should equal 15, 10 would equal 55, etc. But I get 5 = 25, 10, 100.
Changed to decimal to see if that had anything instead of integer and still did nothing. I saw a few things to set decCount to = 1. Did that and the number was closer but still not there.
Dim decSum As Decimal = 0
Dim decNumber As Decimal = 0
Dim decCount As Decimal = 0
Dim strUserInput As String
strUserInput = InputBox("Enter a positive integer value.", "Input Needed", 0)
If Decimal.TryParse(strUserInput, decNumber) And (decNumber >= 0) Then
Do While decCount < decNumber
decSum = decSum + decNumber
decCount = decCount + 1
Loop
Else
MessageBox.Show("Enter a positive numeric value")
End If
MsgBox("The sum of the numbers 1 through " & decNumber & " is " & decSum)
You are trying to calculate a factorial of a given input, but in your loop you are adding the same number repeatedly (effectively, you are multiplying the number by itself instead of finding the factorial).
Change this line:
decSum = decSum + decNumber
to this:
decSum = decSum + decCount
I need to make an auto loan calculator that is only able to calculate 2.3% interest for 84 months. I've never made a calculator that didn't allow a user to input these amounts, so how do I code that part of the calc?
Dim res As Decimal = 100 ' Current value
Dim interest As Decimal = 1.89 / 100 ' 1.89%
For i As Integer = 1 To 89 'Months
Dim amr As Decimal = res * interest
res += amr
Next
MsgBox(res)
Depends on what you want to calculate.. Monthly paiement, equity, what is left to pay ?
Here's the formula to calculate the monthly paiement of a loan
Dim monthlyPaiement As Decimal
Dim loanAmount As Decimal
Dim interestRate As Decimal
Dim loanLengthInMonth As Decimal
loanAmount = 25000
interestRate = 1.9 / 100
loanLengthInMonth = 84
interestRate /= 12 ' Divide by number of month in a year
monthlyPaiement = loanAmount * ((1 + interestRate) ^ loanLengthInMonth) * interestRate / (((1 + interestRate) ^ loanLengthInMonth) - 1)
Ok I have got this far and if you run it, it will do what you ask. Now when I type in 99999 or -99999 it will not end. Can someone tell me what I am doing wrong. I am suppose to loop until a sentinel value of -99999 is enter for previous meter reading.
Sub Main()
' program to compute a consumer’s electric bill. It will calculate the bill for one or more customers
' by looping until a sentinel value of -99999 is entered for the previous meter reading.
Dim previousReading As Integer = 0
Dim currentReading As Integer = 0
Do While (previousReading <> -99999)
Dim salesTax As Double
' prompt user to input value for previous reading then convert to integer
Console.WriteLine("Enter the value of previous meter reading")
previousReading = Convert.ToInt32(Console.ReadLine())
' prompt user to input value for current reading then convert to integer
Console.WriteLine("Enter the value of current meter reading")
currentReading = Convert.ToInt32(Console.ReadLine())
Dim kwhConsumed As Integer
Dim electricCharge, totalBill As Double
' calculate KWH consumed
kwhConsumed = currentReading - previousReading
' Use select case to determine electricCharge
Select Case kwhConsumed
Case Is < 500
electricCharge = kwhConsumed * 0.05
Case 500 To 1000
electricCharge = 25 + ((kwhConsumed - 500) * 0.055)
Case Is > 1000
electricCharge = 52.5 + ((kwhConsumed - 1000) * 0.06)
End Select
' calculate sales tax
salesTax = electricCharge * 0.085
' calculate total charges
totalBill = electricCharge + salesTax
' Output values for kwhConsumed, electricCharge, salesTax, and totalBill
Console.WriteLine("KWH consumed = " & kwhConsumed & " KWH")
Console.WriteLine("Electric charge = $" & Math.Round(electricCharge, 2))
Console.WriteLine("Sales tax = $" & Math.Round(salesTax, 2))
Console.WriteLine("Total bill = $" & Math.Round(totalBill, 2))
Loop
End Sub
You can try using string comparison instead for previousReading <> -99999. You also need to use absolute value to consider both -99999 and 99999. Do something like this
Do While (previousReading <> 99999)
//code
previousReading = Math.Abs(Convert.ToInt32(Console.ReadLine()))
//code
Loop
I'm guessing this is homework?
Instead of blurting out the answer, I wonder if you might think about inserting a Debug.Print statement and some kind of "break" statement after your previousReading = Convert.ToInt32 statement. To look for the "break" statement, search for "vb.net exit loop" and see what pops up.