9 Replies - 18279 Views - Last Post: 13 August 2012 - 10:03 PM

#1 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

IsNumeric in C#

Posted 05 July 2011 - 11:52 PM

Hello everyone

I was working on a C# project which was checking for string, date and integer datatype in given value.
I have 2 methods for checking numeric value.

1st method is converting value to charArray and checking one by one to validate the character using Char.IsNumeric©.

2nd method is validating value using int.TryParse Method.
1st Method
        static bool IsNumeric(string value)
        {
            try
            {
                char[] chars = value.ToCharArray();
                foreach (char c in chars)
                {
                    if (!char.IsNumber(c))
                        return false;
                }
                return true;
            }
            catch (Exception ex) { return false; }
        }




2nd Method
        static bool IsNumeric(string value)
        {
            try
            {
                int number;
                bool result = int.TryParse(value, out number);
                return result;
            }
            catch (Exception ex) { return false; }
        }



Which one is better?

Is This A Good Question/Topic? 0
  • +

Replies To: IsNumeric in C#

#2 janne_panne  Icon User is offline

  • WinRT Dev
  • member icon

Reputation: 429
  • View blog
  • Posts: 1,047
  • Joined: 09-June 09

Re: IsNumeric in C#

Posted 06 July 2011 - 02:24 AM

int.TryParse is probably a bit slower if you are doing thousands of checks and most of them invalid. If you are planning to use the value after checking if it is numeric, I would only use int.TryParse straight in the code because there is no reason to write custom enclosing methods for everything.

// lots of code here
int val;
if (int.TryParse("123", out val)) {
  // do something with the integer val
}
// more code here



Also if you want to extend your IsNumeric to check decimal values too, it's easier to change int.TryParse to decimal.TryParse than use char.IsNumber because it won't check for dots and commas.

But on the other hand if you have veeeery long integer and just changing int.tryparse to long.tryparse isn't enough, then the IsNumber of char would work better.

Conclusion:
I'd use int.TryParse. But depending on your needs, choose the best method.
Was This Post Helpful? 1
  • +
  • -

#3 wiero  Icon User is offline

  • D.I.C Head

Reputation: 48
  • View blog
  • Posts: 78
  • Joined: 29-June 11

Re: IsNumeric in C#

Posted 06 July 2011 - 03:23 AM

hello,

i agree with janne_panne it's better to use method 2. Microsoft also suggest to do it that way
http://msdn.microsof...y/bb384043.aspx
also first method will fail for strings containg whitespaces and second won't, checking other numeric types would require more complex string manipulation

i think you can remove try catch from method 2 besacuse there is no way it could throw an excaption even if string is null, TryParse methods are designed to be "safe"
Was This Post Helpful? 1
  • +
  • -

#4 noorahmad  Icon User is offline

  • Untitled
  • member icon

Reputation: 209
  • View blog
  • Posts: 2,290
  • Joined: 12-March 09

Re: IsNumeric in C#

Posted 06 July 2011 - 03:44 AM

Thank you guys
Was This Post Helpful? 0
  • +
  • -

#5 shyams  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 04-August 11

Re: IsNumeric in C#

Posted 04 August 2011 - 11:26 PM

VB provides the IsNumeric function to test whether a string contains a valid numeric value or not. C# has no such function leaving you to write your own.

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

This post has been edited by Curtis Rutland: 05 August 2011 - 06:36 AM
Reason for edit:: fixed code tags

Was This Post Helpful? 0
  • +
  • -

#6 ad3285  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 3
  • Joined: 15-August 11

Re: IsNumeric in C#

Posted 16 August 2011 - 03:00 AM

I'll go for Method 2 as it can handles negative and decimal numbers.
Was This Post Helpful? 0
  • +
  • -

#7 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 162
  • Joined: 01-August 08

Re: IsNumeric in C#

Posted 16 August 2011 - 07:15 AM

Another way of doing it is using the Microsoft.VisualBasic namespace. You can then use the IsNumeric method in C#.
Was This Post Helpful? 0
  • +
  • -

#8 AdamSpeight2008  Icon User is online

  • MrCupOfT
  • member icon


Reputation: 2251
  • View blog
  • Posts: 9,435
  • Joined: 29-May 08

Re: IsNumeric in C#

Posted 16 August 2011 - 07:30 AM

The VB IsNumeric lets through Hex characters ABCDEF, so it fails if you want digits.
Was This Post Helpful? 0
  • +
  • -

#9 sithius92  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 162
  • Joined: 01-August 08

Re: IsNumeric in C#

Posted 16 August 2011 - 07:31 AM

View PostAdamSpeight2008, on 16 August 2011 - 09:30 AM, said:

The VB IsNumeric lets through Hex characters ABCDEF, so it fails if you want digits.


Ah, I didn't realize that. Good point.
Was This Post Helpful? 0
  • +
  • -

#10 barbary  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 13-August 12

Re: IsNumeric in C#

Posted 13 August 2012 - 10:03 PM

View Postshyams, on 04 August 2011 - 11:26 PM, said:

VB provides the IsNumeric function to test whether a string contains a valid numeric value or not. C# has no such function leaving you to write your own.

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


This is the only really correct answer. As somebody else said if you have a realy long number then int.TryParse will fail for that.

However you then go on to say that handling negitive numbers is a plus which would imply that some non numeric charaters are going to be allowed so its best you think about your use cases and decided which is best.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1