i need to check whether a demical is 0 through 49.99 or 50 through 99.99 or 100 through 199.99 or greater than 200. i am trying to do this with select case, but i am not sur
I have my doubts that you've framed this question to say exactly what you mean. Do you really want the first group to encompass just 0 through 49.99? Or do you really mean 0 up to but not including 50, and you simply expect your input to have 2 decimal places or fewer? If you want to group numbers by fifties, say, then it is very strange to write:
Select Case value
Case Is <= 49.99
Debug.WriteLine("49.99 or less")
Case Is <= 99.99
Debug.WriteLine("greater than 49.99, 99.99 or less")
' ... and so on '
End Select
The number 49.995 here falls into the second group, which seems counterintuitive. Picking two decimal places as the cut-off point is arbitrary.
The '<=' operator is not the way to go here; use the '<' operator; it makes a lot more sense:
Select Case value
Case Is < 50
Debug.WriteLine("less than fifty")
Case Is < 100
Debug.WriteLine("fifty or greater, less than 100")
' ... and so on '
End Select
Structure employee
Dim percent As Decimal
Dim dayname As DayOfWeek
End Structure
Dim emp As employee
emp.percent = CDec(45.5)
emp.dayname = DayOfWeek.Friday
Select Case True
Case (emp.percent >= 0 And emp.percent <= 49.99
And emp.dayname = Now.DayOfWeek)
MsgBox("Employee percentage " & emp.percent
& "Name of the day " & Now.DayOfWeek.ToString)
End Select
I came across this question but these responses still allow too many things to fall in the gaps.
'In this example, a value of 49.991 - 49.999* will fall in the 99.99 category, where I expect it is intended for the 49.99 category
Select Case value
Case Is <= 49.99
Debug.WriteLine("first group")
Case Is <= 99.99
Debug.WriteLine("second group")
Case Is <= 199.99
Debug.WriteLine("third group")
Case Else
Debug.WriteLine("fourth group")
End Select
Instead it is better to reverse the order, to avoid having to specify superfluous decimal places in an effort to close the gaps.
Select Case value
Case Is >= 200
Debug.WriteLine("fourth group")
Case Is >= 100
Debug.WriteLine("third group")
Case Is >= 50
'49.9999* will always fall in this group irrespective of number of decimal places
Debug.WriteLine("second group")
Case Else
Debug.WriteLine("first group")
End Select
The Select Case
statement only follows only the first true case so even though subsequent cases may also be true, they will be bypassed if caught by an earlier case.
Select Case aa
Case 1 To 1.49
MsgBox(1)
Case 1.5 To 2
MsgBox(2)
Case Else
MsgBox("was lower than 1 or higher than 2 or between 1.49 and 1.5")
End Select
this(below) would go into case else
Dim aa As Double = 1.499
this(below) will go into case 1 to 1.49
Dim aa As Double = 1.4
this(below) will go into case 1.5 to 2
Dim aa As Double = 1.78
other way of doing it: From here
Select Case value
Case Is <= 49.99
Debug.WriteLine("first group")
Case Is <= 99.99
Debug.WriteLine("second group")
Case Is <= 199.99
Debug.WriteLine("third group")
Case Else
Debug.WriteLine("fourth group")
End Select
and maybe this too:
Select Case true
Case (value >= 0 andalso value <= 49.99)
Debug.WriteLine("first group")
Case (value >= 50 andalso value <= 99.99)
Debug.WriteLine("second group")
Case (value >= 100 andalso value <= 199.99)
Debug.WriteLine("third group")
Case Else
Debug.WriteLine("fourth group")
End Select
AlbertEin is onto something, but to do integer division like that in VB.Net you have to write it like this:
Dim range as Integer
range = someInteger \ 50
Notice the backwards division symbol. From there you can Select Case range
.
But really, jvanderh's answer most expresses what you want to do, because it allows for easy addition of cases in the future that don't break on a multiple of 50 and don't require future maintainers to follow the math or know about the \ operator.
This is how I would do it, I use the # to explicitly state the values are of type "double".
Dim input As Double = 2.99
Select Case input
Case 0.0# To 49.99#
Response.Write("Between 0 to 49.99")
Case 50.0# To 99.99#
Response.Write("Between 50 and 99.99")
Case Else
Response.Write("The value did not fall into a range.")
End Select