Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if IsNumeric [duplicate]

Tags:

c#

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.

like image 924
user1202606 Avatar asked Mar 21 '12 17:03

user1202606


People also ask

What can I use instead of Isnumeric?

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.

What is Isnumeric value?

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.

How do you check if it is a number VBA?

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.


1 Answers

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);     } } 
like image 141
Otávio Décio Avatar answered Oct 12 '22 01:10

Otávio Décio