Possible Duplicate:
How to identify if string contain a number?
In VB there's an IsNumeric function, is there something similar in c#?
To get around it, I just wrote the code:
if (Int32.Parse(txtMyText.Text.Trim()) > 0)
I was just wondering if there is a better way to do this.
Avoid using the IsNumeric() function, because it can often lead to data type conversion errors, when importing data. On SQL Server 2012 or later, use the Try_Convert() or Try_Cast() function instead. On earlier SQL Server versions, the only way to avoid it is by using LIKE expressions.
Returns a Boolean value indicating whether an expression can be evaluated as a number. The required expressionargument is a Variant containing a numeric expression or string expression. IsNumeric returns True if the entire expression is recognized as a number; otherwise, it returns False.
The ISNUMERIC function returns TRUE if the value is a valid number. The ISNUMERIC function returns FALSE if the value is not a valid number.
You could write an extension method:
public static class Extension { public static bool IsNumeric(this string s) { float output; return float.TryParse(s, out output); } }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With