Is there any IsNumeric in C#?


S

Steve Kershaw

Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(string)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve
 
Ad

Advertisements

J

Juan T. Llibre

Import Microsoft.VisualBasic and use it!

if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
//Do Something
}

if (Microsoft.VisualBasic.Information.IsNumeric("yadda"))
{
//Do Something
}
 
G

Guest

Steve,

Try this:

public static bool IsInteger(string theValue)
{
try
{
Convert.ToInt32(theValue);
return true;
}
catch
{
return false;
}
}
}
 
J

Juan T. Llibre

From Scott Hanselman's blog at :

http://www.hanselman.com/blog/ExploringIsNumericForC.aspx

Actually, this doesn't really do what IsNumeric does,
as IsNumeric should also return true for floating point numbers.

That code does not return true for floating point numbers,
which -nevertheless- *are* numbers.

That is really more of an "IsInt".

Importing Microsoft.VisualBasic does the complete job.
 
K

Kevin Spencer

In the .Net 2.0 CLR, there is an Int32.TryParse() method that doesn't throw
an exception, and you can use that. In .Net 1.1, you're pretty much stuck
with the alternatives that the others have already proposed.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
A watched clock never boils.
 
S

Steve C. Orr [MVP, MCSD]

There is no equivalent for this function in C#. VB.NET has lots of
functions that C# developers have to create manually.
Or, you can import the visual basic namespace and use its IsNumeric function
from within C#.
 
Ad

Advertisements

Joined
Apr 11, 2009
Messages
1
Reaction score
0
Hey Guysw Check it out

Here m posting you IsNumeric function,tried too hard but got success at the end...:driver:

public static bool IsNumeric(object numberString)
{
char[] ca = numberString.ToString().ToCharArray();
for (int i = 0; i < ca.Length; i++)
{
if (!char.IsNumber(ca))
if (ca != '.')
return false;
}
if (numberString.ToString().Trim() == "")
return false;
return true;
}
 
Joined
Jun 2, 2009
Messages
1
Reaction score
0
Yes, there is no equivilant function in C#, but there is a very nice blog on this subject.

Look at dotnet.org.za/deonvs/archive/2004/07/06/2579.aspx

Very helpfull info on this. I cannot link in any links, so can you have to copy the link
 
Joined
Jun 22, 2011
Messages
1
Reaction score
0
hi,

there is a tryparse for double, so if you use that, choose the "NumberStyles.Integer" option and check that the resulting double is within the boundaries of Int32, you can determine if you string is an integer without throwing an exception.

hope this helps

private bool TryIntParse(string txt)
{
try
{
double dblOut = 0;
if (double.TryParse(txt, System.Globalization.NumberStyles.Integer
, System.Globalization.CultureInfo.CurrentCulture, out dblOut))
{
// determined its an int, now check if its within the Int32 max min
return dblOut > Int32.MinValue && dblOut < Int32.MaxValue;
}
else
{
return false;
}
}
catch(Exception ex)
{
throw ex;
}
}
 
Joined
Jan 13, 2012
Messages
2
Reaction score
0
Basically same as above but much simpler:

Code:
public static bool IsNumeric(object o)
{
	double result;
	return o != null && Double.TryParse(o.ToString(), out result);
}
 
Last edited:
Ad

Advertisements

Joined
Feb 3, 2012
Messages
1
Reaction score
0
Hello,
I have a need to see if a string is numeric or alphabetic. I understand
that Visual Basic has a method called "IsNumeric(string)" but C#
dosen't appear to have one. Any ideas?

Thanks for your help

Steve

Just try this in C#:
int num;
if(Int.TryParse(String, out number)
{
//Do Something
}
else
{
//Do Something
}
 
Ad

Advertisements


Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top