iBlog

Trevor Johnson
posts - 6 , comments - 12 , trackbacks - 0

My Links

News

Archives

C# IsNumeric

 My replacement for the the VB Function IsNumeric... 

 public static Boolean IsNumeric(string stringToTest)

{
    int result;
    if (int.TryParse(stringToTest, out result))
    {
        return true;
    }
    else
    {
        return false;
    }
}
 
Or even better... (thanks Anon)
 
public static Boolean IsNumeric(string stringToTest)
{
    int result;
    return int.TryParse(stringToTest, out result);
}

Print | posted on Tuesday, March 29, 2011 12:53 PM |

Feedback

Gravatar

# re: C# IsNumeric

public static Boolean IsNumeric(string stringToTest)
{
int result;
return int.TryParse(stringToTest, out result);
}
3/29/2011 1:19 PM | Anon
Gravatar

# re: C# IsNumeric

Have you seen this article at CodeProject?
http://www.codeproject.com/KB/cs/csharp-isnumeric.aspx
3/29/2011 10:12 PM | Chris Smith
Gravatar

# re: C# IsNumeric

this is a good trick but what if the string contains a large numeric value, for example some record containing the 12, 14, 16 digits or even longer Numeric IDs. Possibly overflow?
3/12/2012 7:55 PM | Aatif
Gravatar

# re: C# IsNumeric

Hi Aatif

Thanks for the feedback.
Acording to http://msdn.microsoft.com/en-us/library/f02979c7.aspx...
TryParse method does not throw an exception if the conversion fails.
And...
The conversion fails if the s parameter is Nothing, is not of the correct format, or represents a number less than MinValue or greater than MaxValue.

HTH.
3/13/2012 9:50 AM | TJ
Gravatar

# re: C# IsNumeric

Thanks for ur code... its work perfectly....
8/22/2012 10:34 PM | Brinda
Gravatar

# re: C# IsNumeric

Replacement for IsNumeric

"123".All(c => char.IsDigit(c))

12/3/2012 8:53 PM | Excona
Gravatar

# re: C# IsNumeric

What about other data types than integer (http://msdn.microsoft.com/en-us/library/cs7y5x0x(v=vs.90).aspx). Shouldn't you test the string against all the numeric data types? Here's your method extended to support also floats and decimals.
public static bool IsNumeric(string stringToTest)
{
int i; float f; decimal d;
return int.TryParse(stringToTest, out i) ||
float.TryParse(stringToTest, out f) ||
decimal.TryParse(stringToTest, out d);
}
1/4/2013 2:04 AM | kermamoottori
Gravatar

# re: C# IsNumeric

public static Boolean IsNumeric(TextBox txtBox)
{
int result;
return int.TryParse(txtBox.Text, out result);
}
4/29/2013 7:50 PM | learner
Gravatar

# re: C# IsNumeric

I wrote something a while back that will allow the user to specify a character array of appropriate digits, decimal and sign. Then, use the array with a lambda expression:


public static char[] NumericChars = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '.', '-' };

public bool IsNumeric(string Value)
{
bool bReturn = false;
if (Value != null)
{
bReturn = Value.ToCharArray().All(item => item.ToString().IndexOfAny(NumericChars) >= 0);
}
return (bReturn);
}

Hope this helps.
5/15/2013 4:23 AM | Rich B.
Post A Comment
Title:
Name:
Email:
Website:
Comment:
Verification:
 
 

Powered by: