select case to check range of a decimal number

后端 未结 9 626
抹茶落季
抹茶落季 2020-12-11 17:37

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

9条回答
  •  没有蜡笔的小新
    2020-12-11 17:57

    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.

提交回复
热议问题