IsNumeric C# equivalent

Discussion in 'ASP .Net' started by Stephan Bour, Nov 5, 2003.

  1. Stephan Bour

    Stephan Bour Guest

    IS there a C# equivalent to the VB IsNumeric function?
     
    Stephan Bour, Nov 5, 2003
    #1
    1. Advertisements

  2. Wim Hollebrandse, Nov 5, 2003
    #2
    1. Advertisements

  3. Stephan Bour

    Stephan Bour Guest

    Actually, IsNumeric does two things: converts a string and returns a boolean
    true if the resulting expression is recognized as a number. Parse only
    converts a string representation of a number to double. I need the boolean
    return to validate a text box entry that can't be easily checked with a
    validator.
    Stephan.
     
    Stephan Bour, Nov 5, 2003
    #3
  4. Stephan Bour

    Stephan Bour Guest

    That's the idea. However, I need to do that on a string. I guess I could use
    the String.ToCharArray() method first or use a loop.
    Thanks,
    Stephan.
     
    Stephan Bour, Nov 5, 2003
    #4
  5. Stephan Bour

    mikeb Guest

    mikeb, Nov 5, 2003
    #5
  6. Stephan Bour

    AlexS Guest

    Check Parse and TryParse methods for corresponding types, Int, Long etc.

    HTH
    Alex
     
    AlexS, Nov 5, 2003
    #6
  7. Stephan Bour

    Stephan Bour Guest

    I apologize, I read TryParse too fast. You're right, it does what I need.
    Thank you,
    Stephan.
     
    Stephan Bour, Nov 5, 2003
    #7
  8. Stephan Bour

    AlexS Guest

    So, check TryParse

    MSDN:
    The TryParse method is like the Parse method, except this method does not
    throw an exception if the conversion fails. If the conversion succeeds, the
    return value is true and the result parameter is set to the outcome of the
    conversion. If the conversion fails, the return value is false and the
    result parameter is set to zero.



    HTH

    Alex
     
    AlexS, Nov 5, 2003
    #8
  9. Stephan Bour

    deeptyranjan

    Joined:
    Jun 20, 2007
    Messages:
    2
    Likes Received:
    0
    Location:
    Pune, Maharastra, INDIA
    C# equivalent to the VB IsNumeric function

    Hi guys i got a way around to get the functionality of IsNumeric(of VB.NET)
    in C#. Please try it out.



    Code (Text):
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    private bool IsTextValidated(string strTextEntry)
            {          
                Regex objNotWholePattern = new Regex("[^0-9]");
                return !objNotWholePattern.IsMatch(strTextEntry);            
            }
     
    deeptyranjan, May 23, 2008
    #9
  10. Stephan Bour

    rexmont

    Joined:
    Jul 24, 2009
    Messages:
    1
    Likes Received:
    0
    returns true for null...

    The function returns true if the value is null.

    So for a workaround;

    Code (Text):
    using System;
    using System.Text;
    using System.Text.RegularExpressions;

    private bool IsTextValidated(string strTextEntry)
    {          
          Regex objNotWholePattern = new Regex("[^0-9]");
          return !objNotWholePattern.IsMatch(strTextEntry)
               [COLOR="Blue"]&& (strTextEntry != ""); [/COLOR]    
    }
     
     
    rexmont, Jul 24, 2009
    #10
  11. Stephan Bour

    Crusoe

    Joined:
    Mar 22, 2011
    Messages:
    1
    Likes Received:
    0
    The old IsNumeric function is still available in .NET

    Add a reference to Microst.Visualbasic and use the Information.IsNumeric function.

    e.g.

    using Microsoft.VisualBasic;
    Code (Text):

        private void button1_Click(object sender, EventArgs e) {
          string s = "1";
          if (Information.IsNumeric(s)) {
            MessageBox.Show("yes it's a number");
          }  
        }
     

    P.S. I know this is a 7 year old thread, but, this page is the first one that appears if you Google for C# IsNumeric and it does not have a correct answer. Just trying to fight pollution of the internet.
     
    Last edited: Mar 22, 2011
    Crusoe, Mar 22, 2011
    #11
  12. Stephan Bour

    JBobbins

    Joined:
    Jan 13, 2012
    Messages:
    2
    Likes Received:
    0
    C# equivalent of IsNumeric

    This seems to work well:

    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
    #12
  13. Stephan Bour

    daniefvh

    Joined:
    Jan 26, 2012
    Messages:
    1
    Likes Received:
    0
    int test = 0;
    if (!int.TryParse(Position,test))
    {
    // Is a Number
    }
    else {
    // Not a number
    }
     
    daniefvh, Jan 26, 2012
    #13
    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.