I have a list of values for example: G1, G2, G2.5, G3, G4, etc..) How can I check a range of these values in c# say if I wanted to see if value was between G1 and G2.5 ?
In Vb.net I can do:
Select Case selectedValue
Case "G1" To "G2.5" //This would be true for G1, G2, and G2.5
How can I do this in c#?
G
from selectedValue
decimal
-
var number = decimal.Parse(selectedValue.Replace("G", ""));
if (number >= 1.0m && number <= 2.5m)
{
// logic here
}
To do string comparison, you could just do this
if (string.Compare(selectedValue, "G1") >= 0 && string.Compare(selectedValue, "G2.5") <= 0)
{
...
}
But to do numeric comparison, you'd have to parse it as a number (double
or decimal
)
var selectedValueWithoutG = selectedValue.Substring(1);
var number = decimal.Parse(selectedValueWithoutG);
if (number >= 1D && number <= 2.5D)
{
...
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With