Results 1 to 8 of 8

Thread: C# : isNumeric (FW 1.1)

  1. #1

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    C# : isNumeric (FW 1.1)

    To check if a string is a valid number we either have to use the Try..Catch..Finally with Integer.Parse or try Double.TryParse.

    I just saw a method using regular expressions sometime back
    Code:
    		private bool isNumeric(string stringToCheck) 
    		{
    			System.Text.RegularExpressions.Regex _isNumber = 
    						new System.Text.RegularExpressions.Regex
    						(@"(^[-+]?\d+(,?\d*)*\.?\d*([Ee][-+]\d*)?$)|(^[-+]?\d?(,?\d*)*\.\d+([Ee][-+]\d*)?$)");
    			return _isNumber.Match(stringToCheck).Success;
    		}
    We can call this function like this
    Code:
    if (isNumeric("1234"))
    			{
    				//string is a valid number
    			}
    			else
    			{
    				//string is not a valid number
    			}
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  2. #2
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: C# : isNumeric (FW 1.1)

    Thanks for the code with RegExp.

    Here is a old codebank thread on this topic.

    and here is another way, (using loop and checking every charecter).

    I don't feel easy wth RegExp.
    I find the loop idea easier and easily expandable.
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  3. #3

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: C# : isNumeric (FW 1.1)

    Thanks for posting the links. I did have a look at the one in the first link before posting this.

    Regular expressions are not all that hard.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  4. #4
    PoorPoster iPrank's Avatar
    Join Date
    Oct 2005
    Location
    In a black hole
    Posts
    2,728

    Re: C# : isNumeric (FW 1.1)

    I just tested it. Your code doesn't work with all locales.
    I changed my locale to German (Germany)
    In this locale comma (,) is decimal sign and dot (.) is digit separetor.
    So, in this locale,
    123,456,78.90 is non-numeric and 123.456.78,90 is numeric. But your function returns wrong results.
    How can we check locale in C# ?
    BTW, I'm using FW2.0 .
    Usefull VBF Threads/Posts I Found . My flickr page .
    "I love being married. It's so great to find that one special person you want to annoy for the rest of your life." - Rita Rudner


  5. #5

    Thread Starter
    Shared Member
    Join Date
    May 2005
    Location
    Kashmir, India
    Posts
    2,277

    Re: C# : isNumeric (FW 1.1)

    I haven't tested it with locales. But I will look into it.

    Also I am still stuck to FW 1.1.
    Use [code] source code here[/code] tags when you post source code.

    My Articles

  6. #6
    KrisSiegel.com Kasracer's Avatar
    Join Date
    Jul 2003
    Location
    USA, Maryland
    Posts
    4,985

    Re: C# : isNumeric (FW 1.1)

    VB Code:
    1. public static bool IsNumeric(string ToBeChecked)
    2. {
    3.     double dbl = 0;
    4.     return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
    5. }

    Works better
    KrisSiegel.com - My Personal Website with my blog and portfolio
    Don't Forget to Rate Posts!

    Free Icons: FamFamFam, VBCorner, VBAccelerator
    Useful Links: System.Security.SecureString Managed DPAPI Overview Part 1 Managed DPAPI Overview Part 2 MSDN, MSDN2, Comparing the Timer Classes

  7. #7
    Lively Member lil_bugga's Avatar
    Join Date
    Nov 2006
    Location
    Bishop's Stortford, Hertfordshire, UK
    Posts
    68

    Re: C# : isNumeric (FW 1.1)

    Quote Originally Posted by kasracer
    VB Code:
    1. public static bool IsNumeric(string ToBeChecked)
    2. {
    3.     double dbl = 0;
    4.     return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
    5. }

    Works better
    Hiya, can you explain how this works/what its doing so I can follow it a lil bit easier

    Cheers

  8. #8
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: C# : isNumeric (FW 1.1)

    Quote Originally Posted by lil_bugga
    Hiya, can you explain how this works/what its doing so I can follow it a lil bit easier

    Cheers
    Perhaps you should go to the MSDN library and read the documentation for the Double.TryParse method for yourself.

    I'd also like to point out that the Double.TryParse method was invented in the first place specifically for the implementation of the VB IsNumeric function. The IsDate function in VB.NET 2003 still uses the "parse and catch the exception on failure" approach, which has terrible performance. The authors of VB.NET decided this was acceptable for dates but numbers would be used more often and the performance hit was unacceptable, so Double.TryParse was born. It therefore makes sense to use Double.TryParse in C# too, although this thread was specifically intended to display an alternative, not a replacement.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width