-
Mar 23rd, 2006, 06:51 AM
#1
Thread Starter
Shared Member
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
-
Mar 23rd, 2006, 07:04 AM
#2
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.
-
Mar 23rd, 2006, 07:21 AM
#3
Thread Starter
Shared Member
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
-
Mar 23rd, 2006, 11:08 AM
#4
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 .
-
Mar 24th, 2006, 01:48 AM
#5
Thread Starter
Shared Member
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
-
Apr 16th, 2006, 04:57 AM
#6
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
-
Oct 19th, 2007, 04:43 PM
#7
Lively Member
Re: C# : isNumeric (FW 1.1)
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
-
Nov 4th, 2007, 02:50 AM
#8
Re: C# : isNumeric (FW 1.1)
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|