locked
How to check if a type is Numeric RRS feed

  • Question

  • Can i find out dynamically if a type is numeric or not? i dont want to compare the type with each Numeric DataTypes(ie. mytype == typeof(integer) or mytype == typeof(double)). I just want to seperate the Numeric types(int,long,double etc) in an array from Non-numeric types..
    Thanks in advance..

    Thursday, September 24, 2009 2:25 PM

Answers

  • That just checks to see if a string represents a numeric type.  It doesn't check to see if a value itself is numeric. 

    The numeric types are limited, and there's no quick reflection way to solve this.  You either use a list of the numeric types, or use a list of the primitive non-numeric types, and any type that is primitive, that isn't in the list that contains the non-numeric types would be a numeric type. 

    That being said, it's easier to simply use a list of the numeric types.  It's not that difficult to create one.

    System.Byte
    System.SByte
    System.Int16
    System.UInt16
    System.Int32
    System.UInt32
    System.Int64
    System.UInt64
    System.Single
    System.Double
    System.Decimal


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked as answer by Zest4Quest Thursday, September 24, 2009 2:59 PM
    Thursday, September 24, 2009 2:33 PM
    Moderator

All replies

  • Yep, sure can:

    public bool isNumeric(string val, System.Globalization.NumberStyles NumberStyle)
    {
        Double result;
        return Double.TryParse(val,NumberStyle,
    		System.Globalization.CultureInfo.CurrentCulture,out result);
    }
    For more details see codeproject.
    おろ?
    • Proposed as answer by JohnGrove Thursday, March 24, 2011 4:43 PM
    Thursday, September 24, 2009 2:30 PM
  • That just checks to see if a string represents a numeric type.  It doesn't check to see if a value itself is numeric. 

    The numeric types are limited, and there's no quick reflection way to solve this.  You either use a list of the numeric types, or use a list of the primitive non-numeric types, and any type that is primitive, that isn't in the list that contains the non-numeric types would be a numeric type. 

    That being said, it's easier to simply use a list of the numeric types.  It's not that difficult to create one.

    System.Byte
    System.SByte
    System.Int16
    System.UInt16
    System.Int32
    System.UInt32
    System.Int64
    System.UInt64
    System.Single
    System.Double
    System.Decimal


    Coding Light - Illuminated Ideas and Algorithms in Software
    Coding Light WikiLinkedInForumsBrowser
    • Marked as answer by Zest4Quest Thursday, September 24, 2009 2:59 PM
    Thursday, September 24, 2009 2:33 PM
    Moderator
  • Not as eloquent but you could also just try and perform a mathematical operation and catch the exception.

    foreach (obj in myList)
    {
        try
        {
            obj = obj + 1;
        }

        catch (Exception ex )
        {

        }
    }
    Formerly MichaelTx
    Thursday, September 24, 2009 2:37 PM
  • Woops...he said type not value.  A careless oversight >.<.   ::::Bow to David::::
    おろ?
    Thursday, September 24, 2009 2:37 PM
  • Thanks Guys.. The final requirement was to seperate out the fields in an infragistics grid that are numeric so that i can get the Field Names of Fields that are numeric and non-numeric..
    Thursday, September 24, 2009 3:01 PM
  • Not as eloquent but you could also just try and perform a mathematical operation and catch the exception.

    foreach (obj in myList)
    {
        try
        {
            obj = obj + 1;
        }

        catch (Exception ex )
        {

        }
    }
    Formerly MichaelTx

    It won't compile unless you already know that obj is numeric.
    Thursday, September 24, 2009 3:59 PM
  • I left a few steps out:

                ArrayList myList = new ArrayList();
                
                myList.Add(1);
                myList.Add(2.9);
                myList.Add(true);
                myList.Add(7);
                myList.Add(-14.05);
                myList.Add("String");
    
                double newobj; 
                foreach( object obj in myList)
                {
                    try
                    {
                        newobj = Double.Parse(obj.ToString());                  
                    }
                    catch (InvalidCastException ie)
                    {
                        Console.WriteLine(ie.Message);
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine(ex.Message);
                    }
                }



    Formerly MichaelTx
    Thursday, September 24, 2009 4:41 PM
  • public static bool IsNumeric(object obj)
    {
      return (obj == null) ? false : IsNumeric(obj.GetType());
    }
    
    public static bool IsNumeric(Type type)
    {
      if (type == null)
        return false;
    
      TypeCode typeCode = Type.GetTypeCode(type);
    
      switch (typeCode)
      {
        case TypeCode.Byte:
        case TypeCode.Decimal:
        case TypeCode.Double:
        case TypeCode.Int16:
        case TypeCode.Int32:
        case TypeCode.Int64:
        case TypeCode.SByte:
        case TypeCode.Single:
        case TypeCode.UInt16:
        case TypeCode.UInt32:
        case TypeCode.UInt64:
          return true;
      }
      return false;
    }
    
    Thursday, March 24, 2011 4:08 PM