Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net (http://www.velocityreviews.com/forums/f29-asp-net.html)
-   -   Is there any IsNumeric in C#? (http://www.velocityreviews.com/forums/t112796-is-there-any-isnumeric-in-c.html)

Steve Kershaw 11-03-2005 08:13 PM

Is there any IsNumeric in C#?
 
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


Juan T. Llibre 11-03-2005 08:22 PM

Re: Is there any IsNumeric in C#?
 
Import Microsoft.VisualBasic and use it!

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

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



Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Espaņol : http://asp.net.do/foros/
======================================
"Steve Kershaw" <steve_kershaw@yahoo.com> wrote in message
news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com...
> 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
>




=?Utf-8?B?RGF2ZQ==?= 11-03-2005 08:30 PM

RE: Is there any IsNumeric in C#?
 
Steve,

Try this:

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

"Steve Kershaw" wrote:

> 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
>
>


Juan T. Llibre 11-03-2005 08:45 PM

Re: Is there any IsNumeric in C#?
 
From Scott Hanselman's blog at :

http://www.hanselman.com/blog/Explor...mericForC.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.




Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
Foros de ASP.NET en Espaņol : http://asp.net.do/foros/
======================================
"Dave" <Dave@discussions.microsoft.com> wrote in message
news:D7AA7B02-7A4B-4C32-A36B-A63ED0BDB145@microsoft.com...
> Steve,
>
> Try this:
>
> public static bool IsInteger(string theValue)
> {
> try
> {
> Convert.ToInt32(theValue);
> return true;
> }
> catch
> {
> return false;
> }
> }
> }



> "Steve Kershaw" wrote:
>
>> 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
>>
>>




Kevin Spencer 11-03-2005 09:46 PM

Re: Is there any IsNumeric in C#?
 
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.

"Steve Kershaw" <steve_kershaw@yahoo.com> wrote in message
news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com...
> 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
>




Steve C. Orr [MVP, MCSD] 11-04-2005 06:32 AM

Re: Is there any IsNumeric in C#?
 
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#.

--
I hope this helps,
Steve C. Orr, MCSD, MVP
http://SteveOrr.net


"Steve Kershaw" <steve_kershaw@yahoo.com> wrote in message
news:1131048814.240063.207290@g43g2000cwa.googlegr oups.com...
> 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
>




IronGuy 04-11-2009 07:30 AM

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[i]))
if (ca[i] != '.')
return false;
}
if (numberString.ToString().Trim() == "")
return false;
return true;
}

merlin 06-02-2009 12:32 AM

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

jwbruce93 06-22-2011 05:02 AM

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;
}
}

JBobbins 01-13-2012 05:56 PM

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);
}



All times are GMT. The time now is 08:41 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.