.NET string testing for numeric value

Consider the need for a function in C # that will check if a string is a numeric value.

Requirements:

  • should return a boolean value. Function
  • must be able to accept integers, decimals and negatives.
  • do not accept using Microsoft.VisualBasic to call IsNumeric() . Here's a chance to reinvent the wheel, but the exercise is good.

Current implementation:

  //determine whether the input value is a number public static bool IsNumeric(string someValue) { Regex isNumber = new Regex(@"^\d+$"); try { Match m = isNumber.Match(someValue); return m.Success; } catch (FormatException) {return false;} } 

Question : how can this be improved so that the regular expression matches negative and decimal places? Any radical improvements you would make?

+4
source share
7 answers

Above my head - why not just use double.TryParse ? I mean, if you really don't need a regexp solution, which I'm not sure what you really need in this case :)

+19
source

Can you just use .TryParse?

 int x; double y; string spork = "-3.14"; if (int.TryParse(spork, out x)) Console.WriteLine("Yay it an int (boy)!"); if (double.TryParse(spork, out y)) Console.WriteLine("Yay it an double (girl)!"); 
+10
source
 Regex isNumber = new Regex(@"^[-+]?(\d*\.)?\d+$"); 

Updated to + or - before the number.

Edit: your try block does nothing, since none of the methods inside it actually throw a FormatException . The whole method can be written:

 // Determine whether the input value is a number public static bool IsNumeric(string someValue) { return new Regex(@"^[-+]?(\d*\.)?\d+$").IsMatch(someValue); } 
+6
source

Well, for negatives, you need to include the optional minus sign at the beginning:

 ^-?\d+$ 

For decimals, you need to consider the decimal point:

 ^-?\d*\.?\d*$ 

And a possible exponential notation:

 ^-?\d*\.?\d*(e\d+)?$ 
+1
source

If you really don't want to use regex, Noldorin posted a good extension method in another Q & A.

Update

As Patrick rightly pointed out, the link points to an extension method that checks if an object is numeric or not, and not a numeric value. Then using double.TryParse proposed by Saulius and yodaj007 is probably the best choice, handling all kinds of quirks with different decimal separators, thousands of separators and so on. Just wrap it in a nice extension method:

 public static bool IsNumeric(this string value) { double temp; return double.TryParse(value.ToString(), out temp); } 

... and burn:

 string someValue = "89.9"; if (someValue.IsNumeric()) // will be true in the US, but not in Sweden { // wow, it a number! ] 
0
source

I cannot say that I would use regular expressions to check if a string is a numeric value. Slow and hard for such a simple process. I would just run a line one character at a time until I enter an invalid state:

 public static bool IsNumeric(string value) { bool isNumber = true; bool afterDecimal = false; for (int i=0; i<value.Length; i++) { char c = value[i]; if (c == '-' && i == 0) continue; if (Char.IsDigit(c)) { continue; } if (c == '.' && !afterDecimal) { afterDecimal = true; continue; } isNumber = false; break; } return isNumber; } 

The above example is simple and should do the job for most numbers. However, it is not sensitive to culture, but it must be strong enough to make it sensitive to culture.

0
source

In addition, make sure that the resulting code passes the Turkish test:
http://www.moserware.com/2008/02/does-your-code-pass-turkey-test.html

0
source

All Articles