Sales Tax Calculator

• Question

• So I'm working on a Sales tax Calculator. I believe I have the majority of it working.. but this is my issue. I have two pre-set sales button. One that is for 6.25% and the other is 7%.  Let's say if I type down the sale amount of \$20 and click on the Sales Tax for Massachusetts (6.25) it will tell me that the tax amount for \$20 is \$1.25. Let's say if I input \$21.. the tax amount comes out to \$1.3125. Long story short, I'm trying to have it round to the nearest 10th so it only shows up as \$1.31 or what ever it is suppose to say..

Now, for the second button (7%) If I type down \$20.. The sales tax amount is \$1.4, not \$1.40.

Any ideas?

Thank you

```Class frmCalcSalesTax

Private Sub frmCalcSalesTax_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub BtnMassTax_Click(sender As Object, e As EventArgs) Handles BtnMassTax.Click

Dim decsales As Decimal
Dim decmass As Decimal

Decimal.TryParse(TxtNumIn.Text, decsales)
decmass = decsales * 0.0625
Me.LblNumOut.Text = Convert.ToString(decmass)
End Sub

Private Sub BtnRhodeIslandTax_Click(sender As Object, e As EventArgs) Handles BtnRhodeIslandTax.Click

Dim decsales As Decimal
Dim decri As Decimal

Decimal.TryParse(TxtNumIn.Text, decsales)
decri = decsales * 0.07
Me.LblNumOut.Text = Convert.ToString(decri)
End Sub

Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
End Class```
Thursday, October 12, 2017 2:15 AM

• Hi

OK, this is the code amended for your button names. This code should replace both your existing button click subs. This one sub willhandle both buttons. You *shouldn't* need to alter the buttons on the Form, but let me know if any issues.

```    Private Sub Button_Click(sender As Object, e As EventArgs) Handles BtnMassTax.Click, BtnRhodeIslandTax.Click
Dim b As Button = DirectCast(sender, Button)
Dim decsales As Decimal = 0D
Dim decmass As Decimal = 0D
Decimal.TryParse(TxtNumIn.Text, decsales)
Select Case b.Name
Case "BtnMassTax"
decmass = CDec(decsales * 0.0625)
Case "BtnRhodeIslandTax"
decmass = CDec(decsales * 0.07)
End Select
LblNumOut.Text = decmass.ToString("C")
End Sub```

Regards Les, Livingston, Scotland

• Marked as answer by Thursday, October 12, 2017 3:06 AM
Thursday, October 12, 2017 3:04 AM

All replies

• Hi

One way might be like this. It replaces both Button Subs with this one. This one will handle both buttons

NOTE: change the button names in this code according to your needs - I just used default Button1 and Button2.

```    Private Sub Button_Click(sender As Object, e As EventArgs) Handles Button1.Click, Button2.Click
Dim b As Button = DirectCast(sender, Button)
Dim decsales As Decimal = 0D
Dim decmass As Decimal = 0D
Decimal.TryParse(TxtNumIn.Text, decsales)
Select Case b.Name
Case "Button1"
decmass = CDec(decsales * 0.0625)
Case "Button2"
decmass = CDec(decsales * 0.07)
End Select
LblNumOut.Text = decmass.ToString("0.00")
' if you want currency symbol then  ' replace previous line with   '  LblNumOut.Text = decmass.ToString("C")  End Sub```

Regards Les, Livingston, Scotland

• Edited by Thursday, October 12, 2017 2:43 AM
• Marked as answer by Thursday, October 12, 2017 3:01 AM
• Unmarked as answer by Thursday, October 12, 2017 3:06 AM
Thursday, October 12, 2017 2:40 AM
• Thank you for your reply.. I'm kind of a noob at this. So does this mean I delete

```Private Sub BtnMassTax_Click(sender As Object, e As EventArgs) Handles BtnMassTax.Click

Dim decsales As Decimal
Dim decmass As Decimal

Decimal.TryParse(TxtNumIn.Text, decsales)
decmass = decsales * 0.0625
Me.LblNumOut.Text = Convert.ToString(decmass)
End Sub

Private Sub BtnRhodeIslandTax_Click(sender As Object, e As EventArgs) Handles BtnRhodeIslandTax.Click

Dim decsales As Decimal
Dim decri As Decimal

Decimal.TryParse(TxtNumIn.Text, decsales)
decri = decsales * 0.07
Me.LblNumOut.Text = Convert.ToString(decri)
End Sub```

And just replace it with what you sent? Also, does this mean I have to delete both of the buttons I created and just have one in the gui? I'm trying to understand how one button takes care of both.. Or is it the code that takes care of both buttons?

Thursday, October 12, 2017 2:50 AM
• Hi

Wait a minute and I will do the renaming of the code to suit your button names .....

Regards Les, Livingston, Scotland

Thursday, October 12, 2017 2:56 AM
• It's working!!!! You're a life savor!!! this code works here. Please feel free to let me know if I'm missing anything.

```Class frmCalcSalesTax

Private Sub frmCalcSalesTax_Load(sender As Object, e As EventArgs) Handles MyBase.Load

End Sub

Private Sub Button_Click(sender As Object, e As EventArgs) Handles BtnMassTax.Click, BtnRhodeIslandTax.Click
Dim b As Button = DirectCast(sender, Button)
Dim decsales As Decimal = 0D
Dim decmass As Decimal = 0D
Decimal.TryParse(TxtNumIn.Text, decsales)
Select Case b.Name
Case "BtnMassTax"
decmass = CDec(decsales * 0.0625)
Case "BtnRhodeIslandTax"
decmass = CDec(decsales * 0.07)
End Select
LblNumOut.Text = decmass.ToString("0.00")
End Sub

Private Sub BtnExit_Click(sender As Object, e As EventArgs) Handles BtnExit.Click
Me.Close()
End Sub
End Class```

Thursday, October 12, 2017 2:59 AM
• Hi

OK, this is the code amended for your button names. This code should replace both your existing button click subs. This one sub willhandle both buttons. You *shouldn't* need to alter the buttons on the Form, but let me know if any issues.

```    Private Sub Button_Click(sender As Object, e As EventArgs) Handles BtnMassTax.Click, BtnRhodeIslandTax.Click
Dim b As Button = DirectCast(sender, Button)
Dim decsales As Decimal = 0D
Dim decmass As Decimal = 0D
Decimal.TryParse(TxtNumIn.Text, decsales)
Select Case b.Name
Case "BtnMassTax"
decmass = CDec(decsales * 0.0625)
Case "BtnRhodeIslandTax"
decmass = CDec(decsales * 0.07)
End Select
LblNumOut.Text = decmass.ToString("C")
End Sub```

Regards Les, Livingston, Scotland

• Marked as answer by Thursday, October 12, 2017 3:06 AM
Thursday, October 12, 2017 3:04 AM
• Hi

OK, you seem to have made it work for you.Happy days.

I note you are not interested in using a currency symbol.

Regards Les, Livingston, Scotland

Thursday, October 12, 2017 3:06 AM
• Thank you, it totally makes sense! I'm still debating on whether I want to use a currency symbol, but I see that it works too! I'm not sure if I'm supposed to open up another thread, or if it's okay to answer it here. But if I want to add another box that displays the over all total of the sale plus tax. For example. I would add a label called LblTotalNum that is the total sum of TxtNumIn and LblNumout. Would that be hard?
Thursday, October 12, 2017 3:19 AM
• Thank you, it totally makes sense! I'm still debating on whether I want to use a currency symbol, but I see that it works too! I'm not sure if I'm supposed to open up another thread, or if it's okay to answer it here. But if I want to add another box that displays the over all total of the sale plus tax. For example. I would add a label called LblTotalNum that is the total sum of TxtNumIn and LblNumout. Would that be hard?

Hi

Really should be a new thread.

However, to answer, no it is not difficult.

The first thing to know about is that when dealing with strings containing number, then those strings are NOT NUMBERS, they are still strings. Therefor, you need to change the string to a valid number before you can use it in any calculations.

One way to achive this is by using a Function that you call with a string (of a number) and the Fuction returns a number. The function willreturn zero if the input string can not be parsed to a valid decimal.

Here is a stand alone example of a function to do just that:

```Option Strict On
Option Explicit On
Public Class form1
Private Sub form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim s As String = "123"
Dim d1 As Decimal = GetDecimal(s)

s = "12.3"
Dim d2 As Decimal = GetDecimal(s)

s = "12.34"
Dim d3 As Decimal = GetDecimal(s)

s = "12.3456"
Dim d4 As Decimal = GetDecimal(s)

s = ".3456"
Dim d5 As Decimal = GetDecimal(s)

' to put back into strings (for TextBoxes etc)
' with rounding up

' with 2 decimal places and leading zero
Dim s1 As String = d1.ToString("0.00")

' with 3 decimal places and no leading zero
Dim s2 As String = d5.ToString("#.000")
End Sub
Function GetDecimal(s As String) As Decimal
Dim v As Decimal = 0D
If Decimal.TryParse(s, v) Then Return v
Return 0D
End Function
End Class```
(If you use ("C") in place of any of the other formats shown then it would be currency symbol and 2 decimal places)

Regards Les, Livingston, Scotland

• Edited by Thursday, October 12, 2017 3:36 AM
Thursday, October 12, 2017 3:32 AM
• Sweet. This is helping me understand a lot. Thank you for your help @leshay! I hope you enjoy the rest of your week!
Thursday, October 12, 2017 3:40 AM