Skip to content

Instantly share code, notes, and snippets.

@brazilnut2000
Last active January 2, 2016 00:29
Show Gist options
  • Save brazilnut2000/8223589 to your computer and use it in GitHub Desktop.
Save brazilnut2000/8223589 to your computer and use it in GitHub Desktop.
C#: Determine whether a string is numeric
public static bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
{
Double result;
return Double.TryParse(val, NumberStyle,
System.Globalization.CultureInfo.CurrentCulture, out result);
}
/*
USAGE:
static void Main(string[] args)
{
var testThis = "N/A";
// or
var testThis = "1234.234";
if (isNumeric(testThis, NumberStyles.Any))
{
Console.WriteLine("I am numeric! {0}", testThis);
}
Console.ReadKey();
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment