vb.net 选择大小写来检查十进制数的范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1009902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
select case to check range of a decimal number
提问by l--''''''---------''''''''''''
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 sure of the syntax. please help!
我需要检查 demical 是 0 到 49.99 还是 50 到 99.99 或 100 到 199.99 或大于 200。我正在尝试使用 select case 来执行此操作,但我不确定语法。请帮忙!
回答by Fredou
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
这个(下面)将进入案例1到1.49
Dim aa As Double = 1.4
this(below) will go into case 1.5 to 2
这个(下面)将进入案例1.5到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
回答by jvanderh
Dim value As Double = 133.5
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
Where do values as 49.992 fall in your question? Since you said 0-49.99 and then 50-99.99 anything between 49.99 and 50 where does it go? In my example above it would be included in one of the options so it is values between 0 and 49.99, values between 49.99 and 99.99, etc, etc.
您的问题中 49.992 的值在哪里?既然你说 0-49.99 然后是 50-99.99,那么 49.99 和 50 之间的任何东西都去哪儿了?在我上面的示例中,它将包含在选项之一中,因此它的值介于 0 和 49.99 之间,值介于 49.99 和 99.99 之间,等等。
回答by Dan Tao
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 just0 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:
我怀疑您是否已经提出了这个问题来准确表达您的意思。难道你真的想第一组包括刚刚通过49.99 0?或者您的意思是 0 到 50 但不包括 50,并且您只是希望您的输入有 2 个小数位或更少?如果你想按五十年代对数字进行分组,比如,那么写起来很奇怪:
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.995here falls into the second group, which seems counterintuitive. Picking two decimal places as the cut-off point is arbitrary.
这里的数字49.995属于第二组,这似乎违反直觉。选择两位小数作为分界点是任意的。
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
回答by Joel Coehoorn
AlbertEin is onto something, but to do integer division like that in VB.Net you have to write it like this:
AlbertEin 正在做一些事情,但是要在 VB.Net 中进行这样的整数除法,您必须这样编写:
Dim range as Integer
range = someInteger \ 50
Notice the backwards division symbol. From there you can Select Case range
.
注意向后除法符号。从那里你可以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.
但实际上,jvanderh 的回答最能表达您想要做的事情,因为它允许将来轻松添加不会破坏 50 倍数的案例,并且不需要未来的维护者遵循数学或了解\操作员。
回答by albertein
Dim range as Integer
range = someInteger / 50
'So, if range = 0 is 0-49.99, if it's 1 it's 50 to 99.99, etc
回答by eKek0
Why don't you try if/then/else? They are equivalent, and I am not sure if case statement in VBasic can handle non-integers values.
你为什么不试试if/then/else?它们是等价的,我不确定 VBasic 中的 case 语句是否可以处理非整数值。
回答by Zachary
This is how I would do it, I use the # to explicitly state the values are of type "double".
这就是我的做法,我使用 # 来明确说明值的类型为“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
回答by Muhammad Farooq
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
回答by torpid prey
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
语句仅遵循第一个真实案例,因此即使后续案例也可能为真,但如果被较早的案例捕获,它们将被绕过。