PDA

Click to See Complete Forum and Search --> : C# : isNumeric (FW 1.1)


Shuja Ali
Mar 23rd, 2006, 05:51 AM
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 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 if (isNumeric("1234"))
{
//string is a valid number
}
else
{
//string is not a valid number
}

iPrank
Mar 23rd, 2006, 06:04 AM
Thanks for the code with RegExp. :thumb:

Here is a old codebank thread on this topic.

and here (http://dotnet.org.za/deonvs/archive/2004/07/06/2579.aspx) is another way, (using loop and checking every charecter).

I don't feel easy wth RegExp. :D
I find the loop idea easier and easily expandable.

Shuja Ali
Mar 23rd, 2006, 06:21 AM
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.

iPrank
Mar 23rd, 2006, 10:08 AM
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 .

Shuja Ali
Mar 24th, 2006, 12:48 AM
I haven't tested it with locales. But I will look into it.

Also I am still stuck to FW 1.1. :mad:

Kasracer
Apr 16th, 2006, 04:57 AM
public static bool IsNumeric(string ToBeChecked)
{
double dbl = 0;
return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
}


Works better

lil_bugga
Oct 19th, 2007, 04:43 PM
public static bool IsNumeric(string ToBeChecked)
{
double dbl = 0;
return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
}


Works better

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

Cheers

jmcilhinney
Nov 4th, 2007, 01:50 AM
Hiya, can you explain how this works/what its doing so I can follow it a lil bit easier

CheersPerhaps 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.