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

  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, Nov 3, 2005
    #2
    1. Advertisements

  3. Steve Kershaw

    Guest Guest

    Steve,

    Try this:

    public static bool IsInteger(string theValue)
    {
    try
    {
    Convert.ToInt32(theValue);
    return true;
    }
    catch
    {
    return false;
    }
    }
    }
     
    Guest, 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, 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.
     
    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#.
     
    Steve C. Orr [MVP, MCSD], Nov 4, 2005
    #6
  7. Steve Kershaw

    IronGuy

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

    merlin

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

    jwbruce93

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

    JBobbins

    Joined:
    Jan 13, 2012
    Messages:
    2
    Likes Received:
    0
    Basically same as above but much simpler:

    Code (Text):
    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
    Likes Received:
    0
    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. 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 (here). After that, you can post your question and our members will help you out.