Is there any IsNumeric in C#?

Discussion in 'ASP .Net' started by Steve Kershaw, Nov 3, 2005.

  1. 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 Kershaw, Nov 3, 2005
    #1
    1. Advertising

  2. Import Microsoft.VisualBasic and use it!

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

    if (Microsoft.VisualBasic.Information.IsNumeric("yadda"))
    {
    //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" <> wrote in message
    news:...
    > 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, Nov 3, 2005
    #2
    1. Advertising

  3. 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
    >
    >
    =?Utf-8?B?RGF2ZQ==?=, Nov 3, 2005
    #3
  4. 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.




    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" <> wrote in message
    news:D...
    > 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, Nov 3, 2005
    #4
  5. 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" <> wrote in message
    news:...
    > 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, Nov 3, 2005
    #5
  6. 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" <> wrote in message
    news:...
    > 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], Nov 4, 2005
    #6
  7. Steve Kershaw

    IronGuy

    Joined:
    Apr 11, 2009
    Messages:
    1
    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;
    }
    IronGuy, Apr 11, 2009
    #7
  8. Steve Kershaw

    merlin

    Joined:
    Jun 2, 2009
    Messages:
    1
    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
    merlin, Jun 2, 2009
    #8
  9. Steve Kershaw

    jwbruce93

    Joined:
    Jun 22, 2011
    Messages:
    1
    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;
    }
    }
    jwbruce93, Jun 22, 2011
    #9
  10. Steve Kershaw

    JBobbins

    Joined:
    Jan 13, 2012
    Messages:
    2
    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: Jan 13, 2012
    JBobbins, Jan 13, 2012
    #10
  11. Steve Kershaw

    Bill.Kanzaki

    Joined:
    Feb 3, 2012
    Messages:
    1
    Just try this in C#:
    int num;
    if(Int.TryParse(String, out number)
    {
    //Do Something
    }
    else
    {
    //Do Something
    }
    Bill.Kanzaki, Feb 3, 2012
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stephan Bour

    IsNumeric C# equivalent

    Stephan Bour, Nov 5, 2003, in forum: ASP .Net
    Replies:
    12
    Views:
    243,884
    daniefvh
    Jan 26, 2012
  2. martin
    Replies:
    3
    Views:
    10,586
    Kevin Spencer
    Jul 19, 2004
  3. =?Utf-8?B?TXJNaWtl?=

    IsNumeric Function

    =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    4,837
    Dan Bass
    Nov 29, 2004
  4. David Lozzi

    Name 'IsNumeric' is not declared???

    David Lozzi, Dec 6, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    4,423
    David Lozzi
    Dec 7, 2004
  5. =?Utf-8?B?RGF2ZQ==?=

    Import Microsoft.VisualBasic for IsNumeric?

    =?Utf-8?B?RGF2ZQ==?=, Jan 16, 2006, in forum: ASP .Net
    Replies:
    8
    Views:
    2,958
    Lau Lei Cheong
    Jan 17, 2006
Loading...

Share This Page