M G Vinod Kumar\’s Technology Blog

June 12, 2006

IsNumeric in C#

Filed under: .NET, Uncategorized — mgvinod @ 4:09 pm

C# doesn't have a IsNumeric or IsNumber on string object. So here are some implementations

Using Parse

public bool IsNumeric(string s)
{
      try
      {
            Int32.Parse(s);
      }
      catch
      {
            return false;
      }
      return true;
}
 

Using char 

internal static bool IsNumeric(string numberString)
{
    foreach (char c in numberString)
   {
        if (!char.IsNumber(c))
             return false;
   }
   return true;
}

Using Regex

static bool IsNumeric(string inputString)
{
    return Regex.IsMatch(inputString, "^[0-9]+$");
}

If you are using C# 2.0, you may use TryParse Method

public static bool IsNumeric(object Expression)
{
    bool isNum;
   double retNum;
   isNum = Double.TryParse(Convert.ToString(Expression),   System.Globalization.NumberStyles.Any,System.Globalization.NumberFormatInfo.InvariantInfo, out retNum );
   return isNum;

Advertisements

14 Comments »

  1. Good One

    Comment by Ravi — October 31, 2006 @ 3:57 pm

  2. Excellent

    Comment by RaviKumarBhuvanagiri — October 31, 2006 @ 4:01 pm

  3. I found this on http://www.bigbold.com/snippets/posts/show/2542 and think this will run faster since it does not require an object parameter type:

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

    Comment by David Rudd — January 15, 2007 @ 3:28 pm

  4. Gr8 yar

    Comment by Sabita — November 20, 2007 @ 12:54 pm

  5. Great Stuff…

    Comment by sathish — March 4, 2008 @ 11:55 am

  6. Works like a charm.

    Comment by Issa — April 3, 2008 @ 3:35 pm

  7. Nice, simple and works!

    Comment by Take one for the Gipper — April 22, 2008 @ 5:36 pm

  8. Hey man,

    I’m just an begginner programmer in c# doing it for uni, I needed a isNumeric method so I googled it and found ure site n method.

    I might be completely wrong but you method doesn’t work properly.

    you see if you pass down a string with a number in the middle somewhere(for e.g. wo2k), then it doesn’t catch that error.

    so here’s code that I came up with.

    public bool IsNumeric(string s)
    {

    bool containsNum = false;

    int count = s.Length;
    int numOfAlphabets = 0;

    for (int i = 0; i < count; i++)
    {
    string letter = s.Substring(i, 1);
    listBox1.Items.Add(letter);

    try
    {
    Convert.ToInt32(letter);
    }
    catch
    {
    numOfAlphabets++;
    }
    }

    if (count == numOfAlphabets)
    {
    containsNum = false;
    }
    else { containsNum = true; }

    return containsNum;
    }

    Like I said I might be completely wrong, but I thought I’d share it with you.

    Comment by Aman Singh — June 3, 2008 @ 12:44 pm

  9. UPDATE:

    SORRY GUYS THE ONE ABOVE DIDN’T WORK COMPLETELY. HERE’S THE LATEST ONE:

    public bool IsNumeric(string s)
    {

    bool containsNum = false;

    int count = s.Length;
    int numOfAlphabets = 0;

    for (int i = 0; i < count; i++)
    {
    string letter = s.Substring(i, 1);

    try
    {
    Convert.ToInt32(letter);
    }
    catch
    {
    numOfAlphabets++;
    }
    }

    if (count == numOfAlphabets && numOfAlphabets != 0)
    {
    containsNum = false;
    }
    else { containsNum = true; }

    return containsNum;
    }

    Comment by Aman Singh — June 3, 2008 @ 12:49 pm

  10. Thanks This Worked like a charm!

    Comment by DrKarl66 — June 12, 2008 @ 2:58 pm

  11. Horrible way of doing it – Try catch blocks incur overhead, and it is bad form to use them as a logic structure. Don’t do this, it makes you look like a crappy programmer.

    Comment by anon — October 2, 2008 @ 10:58 pm

  12. Hi, just letting you know that your regex version does not work with the $ symbol eg $100, you also missed the decimal point, so 99.99 would fail also…
    Pitty because I thaught it was a good sample, I will just use your tryparse method and will check back another time because I dont know regex and im too lazy to learn it 🙂

    Thanks

    Comment by A Guy — June 4, 2011 @ 9:57 pm

  13. Comment by Another Guy — June 4, 2011 @ 9:57 pm

  14. You post interesting posts here. Your page deserves much bigger audience.
    It can go viral if you give it initial boost, i know useful service that
    can help you, just type in google: svetsern traffic tips

    Comment by Mable — January 2, 2015 @ 4:03 pm


RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Create a free website or blog at WordPress.com.

%d bloggers like this: