Consider the need for a function in C# that will test whether a string is a numeric value.
The requirements:
- must return a boolean.
- function should be able to allow for whole numbers, decimals, and negatives.
- assume no
using Microsoft.VisualBasic
to call intoIsNumeric()
. Here's a case of reinventing 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 regex would match negatives and decimals? Any radical improvements that you'd make?