Re: C# : isNumeric (FW 1.1)
Thanks for the code with RegExp. :thumb:
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. :D
I find the loop idea easier and easily expandable.
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.
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 .
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. :mad:
Re: C# : isNumeric (FW 1.1)
VB Code:
public static bool IsNumeric(string ToBeChecked)
{
double dbl = 0;
return double.TryParse(ToBeChecked , System.Globalization.NumberStyles.Any , null , out dbl);
}
Works better
Re: C# : isNumeric (FW 1.1)
Quote:
Originally Posted by kasracer
VB Code:
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
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.