How to check string is number or not in C#

Hi! Enveryone:

I am new in C#. I want to check whether the user input to a text box is a number or not. What is the string function to check it I do not want to use try and catch.

Thank you very much!

CLC



Answer this question

How to check string is number or not in C#

  • Julio Casal T.

    If you are using the new .NET Framework 2.0 (C# 2005), then below code will work for you:

  • Jennifer Bhamoo

    Exceptions should be just that - exceptions. Would you regard it as exceptional that an expression is passed to this routine that is not numeric

    An IsNumeric in C# which does not rely on unexceptional exception logic is:http://www.tangiblesoftwaresolutions.com/Articles/CSharp%20Equivalent%20to%20IsNumeric.htm



  • BillyTheGate

    In the TextBox_TextChange check the charater by using the following code

    for (int i=0; i<this.textbox1.text.length; i++)

    {

    if (char.IsDigit(this.textbox1.textIdea)==false)

    // give any error

    }



  • Robin Charisse

    Here's a nice implementation from the guys at sadeveloper.net:

    public static System.Boolean IsNumeric (System.Object Expression)
    {
    if(Expression == null || Expression is DateTime)
    return false;

    if(Expression is Int16 || Expression is Int32 || Expression is Int64 || Expression is Decimal || Expression is Single || Expression is Double || Expression is Boolean)
    return true;

    try
    {
    if(Expression is string)
    Double.Parse(Expression as string);
    else
    Double.Parse(Expression.ToString());
    return true;
    } catch {} // just dismiss errors but return false
    return false;
    }
    }


  • BigT4446

    Within your method, an exception occurs for most cases where the expression is not numeric. You catch the exception within the method. The fact that you don't throw the exception further is not the issue.

    Run a test with both methods and you'll see that with relatively few iterations, the one that relies on exceptions is very slow when the expression is not numeric. For example, at fewer than 100 iterations you can see an obvious difference. If you have this "unexceptional exception" approach throughout your code, you will have performance issues.

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter and VB to C++ converter
    Instant J#: VB to J# converter
    Clear VB: Cleans up VB.NET code
    Clear C#: Cleans up C# code



  • Manoj K Srivastava

    Yip, it's the best way


  • Corby

    If you're using .NET 1.1 then I like the following code...

    bool textIsNumeric = true;
    try
    {
    int.Parse(TextBox1.Text);
    }
    catch
    {
    textIsNumeric = false;
    }

    I know you said you didn't want to use try/catch but this is simple and effective.



  • Clive Townsend

    How am I "forcing" an exception I am in fact *preventing* an exception from being thrown.
    Yes, you're right! The TryParse method on double is available in framework 1.1.


  • meatago

    Ah, I see what you mean. Sure, the creation of the exception (by Double.Parse) would be expensive.
    Without using TryParse, however, this is the only way I can see it happening.


  • BanyiX

    Hi!
    Thanks for all of you.
    I tried it and it solve my problem. In .Net 1.1, Double.TryParse use four paremeters so follows:
    Double.TryParse(Str, System.Globalization.NumberStyles.Any, System.Globalization.CultureInfo.CurrentCulture, out Num)

    Thank again!

    CLC

  • root16749

    This will not work if the user hits a character that is not a digit but it could be in a number like the dot '.' character or the negative/minus '-' character.

  • fishandchips

    That's why double.TryParse is a good option.

  • rob_a89

    But within the method you are forcing an exception to occur when the expression is not numeric. Forcing exceptions as part of normal logic flow when there is a more efficient alternative is something I wouldn't recommend.

    Double.TryParse is available in 1.1 (and 1.0 also).

    David Anton
    www.tangiblesoftwaresolutions.com
    Instant C#: VB to C# converter
    Instant VB: C# to VB converter
    Instant C++: C# to C++ converter and VB to C++ converter
    Instant J#: VB to J# converter
    Clear VB: Cleans up VB.NET code
    Clear C#: Cleans up C# code



  • Tim Daley

    "Would you regard it as exceptional that an expression is passed to this routine that is not numeric "

    No, which is exactly why I don't throw the exception. If an exception is caught, the string is obviously NOT numeric, and the method should return false. This is a perfectly acceptable case for not throwing an exception.

    Note that the example you posted will only work on .NET framework 2, where the TryParse methods are available.



  • How to check string is number or not in C#