开发者

Lack of IsNumeric function in C#

开发者 https://www.devze.com 2022-12-31 23:46 出处:网络
One thing that has bothered me about C# since its release was the lack of a generic IsNumeric function.I know 开发者_Python百科it is difficult to generate a one-stop solution to detrmine if a value is

One thing that has bothered me about C# since its release was the lack of a generic IsNumeric function. I know 开发者_Python百科it is difficult to generate a one-stop solution to detrmine if a value is numeric.

I have used the following solution in the past, but it is not the best practice because I am generating an exception to determine if the value is IsNumeric:

public bool IsNumeric(string input)
{
    try
    {
        int.Parse(input);
        return true;
    }
    catch
    {
        return false;
    }
}

Is this still the best way to approach this problem or is there a more efficient way to determine if a value is numeric in C#?


Try this:

int temp;
return int.TryParse(input, out temp);

Of course, the behavior will be different from Visual Basic IsNumeric. If you want that behavior, you can add a reference to "Microsoft.VisualBasic" assembly and call the Microsoft.VisualBasic.Information.IsNumeric function directly.


You can use extension methods to extend the String type to include IsInteger:

namespace ExtensionMethods
{
    public static class MyExtensions
    {
        public static bool IsInteger(this String input)
        {
             int temp;
             return int.TryParse(input, out temp);        
        }
    }   
}


Rather than using int.Parse, you can use int.TryParse and avoid the exception.

Something like this

public static bool IsNumeric(string input)
{
  int dummy;
  return int.TryParse(input, out dummy);
}

More generically you might want to look at double.TryParse.

One thing you should also consider is the potential of handling numeric string for different cultures. For example Greek (el-GR) uses , as a decimal separator while the UK (en-GB) uses a .. So the string "1,000" will either be 1000 or 1 depending on the current culture. Given this, you might consider providing overloads for IsNumeric that support passing the intended culture, number format etc. Take a look at the 2 overloads for double.TryParse.


I've used the following extension method before, if it helps at all:

public static int? AsNumeric(this string source)
{
  int result;
  return Int32.TryParse(source, out result) ? result : (int?)null;
}

Then you can use .HasValue for the bool you have now, or .Value for the value, but convert just once...just throwing it out there, not sure what situation you're using it for afterwards.


If you use Int32.TryParse then you don't need to wrap the call in a TryCatch block, but otherwise, yes that is the approach to take.


Not exactly crazy about this approach, but you can just call the vb.net isNumeric function from C# by adding a reference to the Microsoft.VisualBasic.dll library...

bool x= Microsoft.VisualBasic.Information.IsNumeric("123");

The other approaches given are superior, but wanted to add this for the sake of completeness.


Lot's of TryParse answers. Here's something a bit different using Char.IsNumber():

    public bool IsNumeric(string s)
    {
        for (int i = 0; i < s.Length; i++)
        {
            if (char.IsNumber(s, i) == false)
            {
                return false;
            }
        }
        return true;
    }


Take a look on the following answer: What is the C# equivalent of NaN or IsNumeric?

Double.TryParse takes care of all numeric values and not only ints.


Another option - LINQ!

public static class StringExtensions
{
  public static bool IsDigits(this String text)
  {
    return !text.Any(c => !char.IsDigit(c));
  }
}

Note that this assumes you only want digits 0-9. If you want to accept decimal point, sign, exponent, etc, then repalce IsDigit() with IsNumber().


I've been using the following small code snippet for years as a pure C# IsNumeric function.

Granted, it's not exactly the same as the Microsoft.VisualBasic library's IsNumeric function as that (if you look at the decompiled code) involves lots of type checking and usage of the IConvertible interface, however this small function has worked well for me.

Note also that this function uses double.TryParse rather than int.TryParse to allow both integer numbers (including long's) as well as floating point numbers to be parsed. Also note that this function specifically asserts an InvariantCulture when parsing (for example) floating point numbers, so will correctly identify both 123.00 and 123,00 (note the comma and decimal point separators) as floating point numbers.

using System;
using System.Globalization;

namespace MyNumberFunctions
{
   public static class NumberFunctions
   {
      public static bool IsNumeric(this object expression)
      {
         if (expression == null)
         {
            return false;
         }
         double number;
         return Double.TryParse(Convert.ToString(expression, CultureInfo.InvariantCulture), NumberStyles.Any, NumberFormatInfo.InvariantInfo, out number);
      }
   }
}

Usage is incredibly simple, since this is implemented as an extension method:

string myNumberToParse = "123.00";
bool isThisNumeric = myNumberToParse.IsNumeric();


public bool IsNumeric(string input)
{
   int result;
   return Int32.TryParse(input,out result);
}


try this:

public static bool IsNumeric(object o)
{
    const NumberStyles sty = NumberStyles.Any;
    double d;
    return (o != null && Double.TryParse(o.ToString(), sty, null, out d));
}


You can still use the Visual Basic function in C#. The only thing you have to do is just follow my instructions shown below:

  1. Add the reference to the Visual Basic Library by right clicking on your project and selecting "Add Reference":

Lack of IsNumeric function in C#

  1. Then import it in your class as shown below:

    using Microsoft.VisualBasic;

  2. Next use it wherever you want as shown below:

            if (!Information.IsNumeric(softwareVersion))
        {
            throw new DataException(string.Format("[{0}] is an invalid App Version!  Only numeric values are supported at this time.", softwareVersion));
        }
    

Hope, this helps and good luck!

0

精彩评论

暂无评论...
验证码 换一张
取 消