Check If A String Value Is Numeric

A way to determine if a string value has a specific Style Number And/OR Culture

Introduction

This article will give you a way to determine if a given stringhas a valid number style (Integer, Float, Currency, etc.) according to specified/current culture.

Background

In all VBScript and Visual Basic applications, there is a built-in function called IsNumeric / IsCurrency, but in C# there is no built-in function with the same functionality.I have investigated a little and found out that all given solutions always use trycatch to find out if the given stringvalue can be parsed.

That seemed like a good way to test it, BUT what if the number contains commas or a currency sign? Then I have to add another check to this code and strip down the non numeric sign and test the result.Here I present a simple way that is supported by C#.

The Solution

In the code shown below, I actually use a built-in functionality of C# to determine if a given stringvalue stands up to the requirements I want.

I saw this method and thought it was good. You mentioned about a Regular Expression. To make it easier on those finding this article, if you do want to use a method that utilizes regular expressions, then you can try this.

Don't let the runtime try and parse the string, when if fails it throw an exception which is VERY expensive in processing time, a better way would be to use regular expressions with a simple number pattern match.

Don't let the runtime try and parse the string, when if fails it throw an exception which is VERY expensive in processing time, a better way would be to use regular expressions with a simple number pattern match.

No, No, No, yourself. Read the entire article, he clearly switches to the preferred method of calling TryParse(), which does NOT incur the exception overhead and is fully supportive of any selected culture.

If that's the intention, then the article is written poorly. You have to take into account that people only look at the first part of the article and when they get what they want, they don't continue on with the rest of the article.
I only read this far because I wanted to see why it was rated at a 3 out of 5. I personally use a function in our framework that returns a bool with a passed in string and a regular expression to evaluate the string.
What happens if you need something different than what's in the Number Style? A function that allows a regular expression to be passed in along with the string is more efficient in that it allows for any type of numberic format and isn't limited to what's inside the function... Just a thought, a regular expression is a good approach to this problem.

here is a simple timing comparision:
(Standard: [IsNumeric] is simple for cycle checking if char is number and if not returns false)
Regex:
public static bool IsInteger(String strNumber)
{
Regex objNotIntPattern = new Regex("[^0-9-]");
Regex objIntPattern = new Regex("^-[0-9]+$|^[0-9]+$");

return !objNotIntPattern.IsMatch(strNumber) &&
objIntPattern.IsMatch(strNumber);
}
and From Double: Double.TryParse(val,NumberStyle,System.Globalization.CultureInfo.CurrentCulture,out result);
so think, if you realy want regex for simple checking

There are many ways to code solution for this problem, what I presented here is a built-in solution supported by the .NET frame work. The suggested solution of mine does not require understating or learning Regular Expression which usually fit for experts.

I have removed the so called poor example which may confuse the readers.