How to remove decimal from variable in vb.net - vb.net

I have created a code to work out pay of workers, and to - 1 of pay for charity if it is over 100, and every 10 over 100 -1. But I need to get rid of the decimal with new pay variable. At the end of the if function
Dim donation As Single
Dim newpay As Single
Dim hourlyRate As Decimal
Dim pay As Decimal
Dim hours As Single
Sub Main()
Console.WriteLine("how many hours have you worked this week?")
hours = Console.ReadLine
Console.WriteLine("what is your hourly pay?")
hourlyRate = Console.ReadLine
pay = hours * hourlyRate
newpay = pay
If pay > 100 Then
pay = pay - 1
newpay = newpay - 100
donation = donation + 1
newpay = newpay / 10
Math.Floor(newpay)
pay = pay - newpay
donation = donation + newpay
End If
Console.WriteLine("you earned £" & pay & " and donated £" & donation & " to charity")
Console.ReadLine()
End Sub

Use the Math.Floor function to round down
SO if newpay was 3.6
Math.Floor(newpay)
will return 3
SO
Use the result of this function
donation = donation + Math.Floor(newpay)
OR
newpay = Math.Floor(newpay)

Related

Converting cents into amount of change given

So i am having troubles with this code. I get the dollars part to work correctly but the rest is not. Im trying to change 529 cents into 5 dollars 1 quarter 0 dimes 0 nickels 4 pennies, here is my code.
Option Explicit On
Option Strict On
Module Module1
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
If (amount >= 100) Then
dollars = amount \ 100
amount = amount - (100 * dollars)
ElseIf (amount >= 25) Then
quarters = amount \ quarters
amount = amount - (25 * quarters)
ElseIf (amount >= 10) Then
dimes = amount \ dimes
amount = amount - (10 * dimes)
ElseIf (amount >= 5) Then
nickels = amount \ nickels
amount = amount - (5 * nickels)
ElseIf (amount >= 1) Then
pennies = amount
End If
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.ReadLine()
End Sub
End Module
Write a function to automate the counting of coins. You were almost there. Just use modulo to find the remaining pennies each time a count is done.
Sub Main()
Dim amount As Integer
Dim dollars As Integer
Dim quarters As Integer
Dim dimes As Integer
Dim nickels As Integer
Dim pennies As Integer
Console.WriteLine("Please Enter A Unit of Cents")
amount = CInt(Console.ReadLine())
dollars = parseDenomination(amount, 100)
quarters = parseDenomination(amount, 25)
dimes = parseDenomination(amount, 10)
nickels = parseDenomination(amount, 5)
pennies = amount
Console.WriteLine("" & dollars & " dollars " & quarters & " quarters " & dimes & " dimes " & nickels & " nickels " & pennies & " pennies ")
Console.WriteLine($"{getAmountString(dollars, "Dollar")}, {getAmountString(quarters, "Quarter")}, {getAmountString(dimes, "Dime")}, {getAmountString(nickels, "Nickel")}, {getAmountString(pennies, "Penny")},")
Console.ReadLine()
End Sub
Private Function parseDenomination(ByRef amount As Integer, count As Integer) As Integer
Dim result = amount \ count
amount = amount Mod count
Return result
End Function
Private Function getAmountString(amount As Integer, name As String) As String
Return $"{name}{If(amount = 1, "", "s")}: {amount}"
End Function
I added an additional method to help get the string output. It kind of breaks down for "pennies / pennys" though - that's English for you.

VB program to calculate monthly deposits plus interest

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)

How to code Auto Loan calc with fixed months and interest? (VB)

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)

VB Simple If statements (h/w)

I don't deal with VB. I am helping my mother with her homework and just cant think straight anymore on what to do with this statement.
Private Sub btnTotal_Click(sender As Object, e As EventArgs) Handles btnTotal.Click
Dim intPackA As Integer = 0
Dim intPackB As Integer = 0
Dim intPackC As Integer = 0
Dim SavingsPriceB As Decimal = 0.0
Dim SavingsPriceC As Decimal = 0.0
Dim TotalPrice As Decimal = 0.0
lblTotal.Text = String.Empty
If radPackA.Checked Then
TotalPrice = 9.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackA) Then
If intPackA > 10 Then
TotalPrice = TotalPrice + ((intPackA - 10) * 2)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
SavingsPriceB = 14.95 + ((intPackB - 20) * 1)
SavingsPriceC = 19.95
If TotalPrice < SavingsPriceB And TotalPrice < SavingsPriceC Then
lblTotal.Text = TotalPrice & ", no savings with Package B or C"
End If
End If
ElseIf radPackB.Checked Then
TotalPrice = 14.95
lblTotal.Text = TotalPrice
If Integer.TryParse(txtHours.Text, intPackB) Then
If intPackB > 20 Then
TotalPrice = TotalPrice + ((intPackB - 20) * 1)
lblTotal.Text = TotalPrice
End If
End If
If chkSavings.Checked Then
End If
ElseIf radPackC.Checked Then
TotalPrice = 19.95
lblTotal.Text = TotalPrice
End If
If chkNonprofit.Checked Then
TotalPrice = Format((TotalPrice - ((TotalPrice / 100) * 20)), ".00")
lblTotal.Text = TotalPrice & ControlChars.CrLf & "Non-Profit Organization discount applied"
End If
End Sub
It's the If chkSavings.Checked that's giving me problem.
This is the program as designed. There is a label bellow the packages that displays the total.
When the Potential Savings is checked, it should also display the amount you could save if you use a different package.
So if I put Package A, 5 hours, 20% discount it should say $7.96, no savings with Package B or C. For Package A, 25 hours it should say $39.95, save $20.00 with Package B, and save $20.00 with Package C
The code I have does not print it even the first part.
Package A and less then 10 hours = $9.95, every additional hour is $2.00 more
Package B and less then 20 hours = $14.95, every additional hour is $1.00 more
Package C with unlimited hours is = $19.95
So My question is, what am I doing wrong in my code or how could I achieve what I am looking for.

I cant get the loop right

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.