Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check range of string values that contains a number

Tags:

c#

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#?

like image 658
TMan Avatar asked Mar 22 '13 16:03

TMan


2 Answers

  1. Remove the G from selectedValue
  2. Parse the remaining into a decimal
  3. Implement your logic against the decimal value

-

var number = decimal.Parse(selectedValue.Replace("G", ""));
if (number >= 1.0m && number <= 2.5m)
{
    // logic here
}
like image 88
Kevin Brydon Avatar answered Sep 22 '22 00:09

Kevin Brydon


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)
{
    ...
}
like image 34
p.s.w.g Avatar answered Sep 19 '22 00:09

p.s.w.g