Varidate data type extracted from excel

In the case of extracting data from excel, I want to varidate extracted data whether it is number or not. How can I do this?

Regards

Hey @NatsuOK

You can try Int.TryParse()

int.TryParse("141241", out temp) = true
int.TryParse("232a23", out temp) = false
int.TryParse("12412a", out temp) = false

Look up double.TryParse() if you’re talking about numbers like 1, -2 and 3.14159. Some others are suggesting int.TryParse(), but that will fail on decimals.

double num;
string candidate = "1";

**double.TryParse(candidate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out num)**

or you can do as well

string s = "sdf34";
    Int32 a;
    if (Int32.TryParse(s, out a))
    {
        // Value is numberic
    }  
    else
    {
       //Not a valid number
    }

or

If you just want to check if a string is all digits (without being within a particular number range) you can use:

string test = "12334";
bool allDigits = test.All(char.IsDigit);

Note - you can also consider Regex.IsMatch(input, @“^\d+$”) //This will return true if input is all numbers.

Because sometimes TryParse with a very long string could potentially overflow.

Regards…!!

2 Likes

you could also use the old fashioned isNumeric(“123”)

@aksh1yadav, @theo500,
both worked well! thank you for your kindly reply:blush: