tags:

views:

3351

answers:

8

This seems like a fairly simple question, and I'm surprised not to have required it before.

What is the most efficient way of testing a string input is a numeric (or conversely Not A Number).

I guess I can do a Double.Parse or a regex (see below)

public static bool IsNumeric(this string value)
{
    return Regex.IsMatch(value, "^\\d+$");
}

but I was wondering if there was a implemented way to do it, such as javascript's NaN() or IsNumeric() (was that VB, I can't remember).

+33  A: 

This doesn't have the regex overhead

double myNum = 0;
String testVar = "Not A Number";

if (Double.TryParse(testVar, out myNum)) {
  // it is a number
} else {
  // it is not a number
}

Incidentally, all of the standard data types, with the glaring exception of GUIDs, support TryParse.

Chris Lively
If needed, You can wrap the above code into a more helpful utility method like public static bool IsInteger(string sMaybeANumber)
Gishu
@Gishu: You are right if all you care about is whether the number can convert.
Chris Lively
The only issue with this is the `Number` object in Javascript is a float or integer, so changing to double.TryParse would be a more exact equivalent
Chris S
+3  A: 

VB has IsNumeric. You could reference Microsoft.VisualBasic.dll & use that function.

shahkalpesh
Can you only get that VB method in > 2.0 versions of .net?
Ed Swangren
ok...I don't know what that means.
Ed Swangren
You can use it from 1.0 on.
Joel Coehoorn
+1  A: 

Yeah, IsNumeric is VB. Usually people use the TryParse() method, though it is a bit clunky. As you suggested, you can always write your own.

int i;
if (int.TryParse(string, out i))
{

}
Ed Swangren
Thanks for the edit Jay, I forgot the 'out'.
Ed Swangren
+13  A: 

I prefer something like this, it lets you decide what NumberStyle to test for.

    public static Boolean IsNumeric(String input, NumberStyles numberStyle)
    {
        Double temp;
        Boolean result = Double.TryParse(input, numberStyle, CultureInfo.CurrentCulture, out temp);
        return result;
    }
Anthony Mastrean
+1 for being the only person so far to Double.TryParse instead of Int.TryParse :)
johnc
+2  A: 

Try this for the sake of change.

static class StringExtensions
{
    public static bool IsNumeric(this string text)
    {
        return text.All(Char.IsNumber);
    }
}
Hasan Khan
This won't work with negative or real numbers.
Stu Mackellar
Yeah, this solution only works for the commutative semiring of cardinals. If we wanted a solution that only worked for *countably* infinitely many inputs, we could have ... uh, yeah, I got no joke here. Yer mom?
Ken
+12  A: 

In addition to the previous correct answers it is probably worth pointing out that "Not a Number" (NaN) in its general usage is not equivalent to a string that cannot be evaluated as a numeric value. NaN is usually understood as a numeric value used to represent the result of an "impossible" calculation - where the result is undefined. In this respect I would say the Javascript usage is slightly misleading. In C# NaN is defined as a property of the single and double numeric types and is used to refer explicitly to the result of diving zero by zero. Other languages use it to represent different "impossible" values.

Stu Mackellar
I did not know that, thanks
johnc
+1  A: 

public static bool IsNumeric(string anyString) { if (anyString == null) { anyString = ""; }

        if (anyString.Length > 0)
        {
            double dummyOut = new double();
            System.Globalization.CultureInfo cultureInfo = new System.Globalization.CultureInfo("en-US", true);
            return Double.TryParse(anyString, System.Globalization.NumberStyles.Any, cultureInfo.NumberFormat, out dummyOut);
        }
        else
        {
            return false;
        }
    }
Mnaouar
+1  A: 

maybe this is a C# 3 feature, but double.NaN;

kenny