# Simple Tax Calculation

• ### Question

• What code would resolve the following problem? I need to charge \$5 per every 1000 for properties between \$0-\$1,4999,999 and \$7 for every 1000 between \$1,500,000 to \$5,000,000 and \$9 for every 1000 for properties above 5,000,001. Here is what I have so far which is not yielding the correct numbers.

If city = "EVANSTON"
If (price <= 1499999)
tax = .005
Else If (price > 1499999 AND price < 5000001)
tax = .007
Else If (price > 5000000)
tax = .009
End If
'Msgbox (tax)
Else if city = "EVANSTON"
tax = .005
End If

Wednesday, February 6, 2019 9:32 PM

### All replies

• Hello,

I have not reviewed the code but have a question, have you worked this calculation out on paper?  Have you used breakpoints to trace through the code to see if the right path is being taken?

Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
VB Forums - moderator

Wednesday, February 6, 2019 9:49 PM
• Excellent point. Thinking about it as just a math problem it would be (Price/1000)*5

So the code would be tax = (price/1000)*5

Wednesday, February 6, 2019 10:47 PM
• Hi

Here is my understanding of what you want.I just have a TextBox1, Label1 and Button1 on Form1 for this test.

```' Form1 with TextBox1, Button1
' and Label1
Option Strict On
Option Explicit On
Public Class Form1
Dim tier1 As Double = 1500000
Dim tier2 As Double = 5000000
TextBox1.Text = "6000000"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim v As Double = 0.0
Dim charge As Double = 0
If Double.TryParse(TextBox1.Text, v) Then
If v > 0 Then
If v >= tier1 Then
charge += tier1 / 1000 * 5
v -= tier1
Else
charge += v / 1000 * 5
v = 0
End If
End If
If v > 0 Then
If v >= (tier2 - tier1) Then
charge += (tier2 - tier1) / 1000 * 7
v -= (tier2 - tier1)
Else
charge += v / 1000 * 7
v = 0
End If
End If
If v > 0 Then
charge += v / 1000 * 9
End If
End If
Label1.Text = charge.ToString
End Sub
End Class```

Regards Les, Livingston, Scotland

Wednesday, February 6, 2019 10:59 PM
• I don't understand this part:
```  If city = "EVANSTON" '*******************
'Do stuff here
Else if city = "EVANSTON" '******************* HUH ?
'Do stuff here
End If
```

Wednesday, February 6, 2019 11:04 PM
• I didn't use all of the code in my example as I was just trying to get that small portion of the code correct. I have a date as a trigger for when to use what I will call the new code for these fees and the confusing elseif in your example it's there to apply the fee of \$5 per 1000 if the date is prior to 1/31/2019. Hopefully that makes sense.

Dim DTE as Date = [Datefield]

If City = "Evanston" and DTE >= #01/31/2019#

If (price <= 1499999)
tax = .005
Else If (price > 1499999 AND price < 5000001)
tax = .007
Else If (price > 5000000)
tax = .009
End If
'Msgbox (tax)
Else if city = "EVANSTON"
tax = .005
End If

• Edited by Thursday, February 7, 2019 12:21 AM
Thursday, February 7, 2019 12:03 AM

After reviewing your code, I don't understand where this code would cover a range of possible inputs. price >= 1499999 AND price < 5000001). Your tiers appear to be assigned one value if I am understanding your code correctly.

Thursday, February 7, 2019 12:15 AM

After reviewing your code, I don't understand where this code would cover a range of possible inputs. price >= 1499999 AND price < 5000001). Your tiers appear to be assigned one value if I am understanding your code correctly.

Hi

Best if you use the 'Quote' reply or indicate who you are replying to as we don't all see a threaded view. (I do realize it is a reply to me as you mention 'tiers')

The example I submitted is just that, an example. There is next to no exception handling, it is just a simple Form that lets you enter the value to calculate the tax for. Did you try it?

Regards Les, Livingston, Scotland

Thursday, February 7, 2019 1:11 AM
• Check the next approach too:

```Dim p As Integer = CInt(Math.Floor(price))
Dim tax As Integer = (Math.Min(p, 1_499_999) \ 1000) * 5
If p >= 1_500_000 Then tax += ((Math.Min(p, 5_000_000) - 1_500_000) \ 1000) * 7
If p >= 5_000_001 Then tax += ((p - 5_000_001) \ 1000) * 9```

Thursday, February 7, 2019 5:58 AM