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

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

  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.

    in article , AlexS at
    wrote on 11/5/03 14:39:

    > Check Parse and TryParse methods for corresponding types, Int, Long etc.
    >
    > HTH
    > Alex
    >
    > "Stephan Bour" <> wrote in message
    > news:BBCE9144.B64C%...
    >> IS there a C# equivalent to the VB IsNumeric function?
    >>

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

    in article , Wim Hollebrandse at
    wrote on 11/5/03 12:42:

    > Try the static IsNumber() method on the Char class.
    > E.g.:
    >
    > char c = 'a';
    > if (Char.IsNumber(c))
    > {
    > //do whatever...
    > }
    >
    > Hope that helps.
    >
    > Cheers,
    > Wim Hollebrandse
    > http://www.wimdows.net
    > http://www.wimdows.com
    >
    > ---
    > Posted using Wimdows.net Newsgroups - http://www.wimdows.net/newsgroups/
    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

    "Stephan Bour" <> wrote in message
    news:BBCE9144.B64C%...
    > IS there a C# equivalent to the VB IsNumeric function?
    >
    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.

    in article #, AlexS at
    wrote on 11/5/03 16:18:

    > 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
    >
    > "Stephan Bour" <> wrote in message
    > news:BBCEA49C.B6D7%...
    >> 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.
    >>
    >> in article , AlexS at
    >> wrote on 11/5/03 14:39:
    >>
    >>> Check Parse and TryParse methods for corresponding types, Int, Long etc.
    >>>
    >>> HTH
    >>> Alex
    >>>
    >>> "Stephan Bour" <> wrote in message
    >>> news:BBCE9144.B64C%...
    >>>> IS there a C# equivalent to the VB IsNumeric function?
    >>>>
    >>>
    >>>

    >>

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

    "Stephan Bour" <> wrote in message
    news:BBCEA49C.B6D7%...
    > 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.
    >
    > in article , AlexS at
    > wrote on 11/5/03 14:39:
    >
    > > Check Parse and TryParse methods for corresponding types, Int, Long etc.
    > >
    > > HTH
    > > Alex
    > >
    > > "Stephan Bour" <> wrote in message
    > > news:BBCE9144.B64C%...
    > >> IS there a C# equivalent to the VB IsNumeric function?
    > >>

    > >
    > >

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

    deeptyranjan

    Joined:
    Jun 20, 2007
    Messages:
    2
    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:
    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
    returns true for null...

    The function returns true if the value is null.

    So for a workaround;

    Code:
    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
    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:
        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
    C# equivalent of IsNumeric

    This seems to work well:

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

    daniefvh

    Joined:
    Jan 26, 2012
    Messages:
    1
    int test = 0;
    if (!int.TryParse(Position,test))
    {
    // Is a Number
    }
    else {
    // Not a number
    }
    daniefvh, Jan 26, 2012
    #13
    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. martin
    Replies:
    3
    Views:
    10,584
    Kevin Spencer
    Jul 19, 2004
  2. =?Utf-8?B?TXJNaWtl?=

    IsNumeric Function

    =?Utf-8?B?TXJNaWtl?=, Nov 29, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    4,835
    Dan Bass
    Nov 29, 2004
  3. 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
  4. Steve Kershaw

    Is there any IsNumeric in C#?

    Steve Kershaw, Nov 3, 2005, in forum: ASP .Net
    Replies:
    10
    Views:
    78,466
    Bill.Kanzaki
    Feb 3, 2012
  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,957
    Lau Lei Cheong
    Jan 17, 2006
Loading...

Share This Page