views:

2156

answers:

5

Title says it all...

But here's an example:

Dim desiredType as Type
if IsNumeric(desiredType) then ...

EDIT: I only know the Type, not the Value as a string.

Ok, so unfortunately I have to cycle through the TypeCode.

But this is a nice way to do it:

 if ((desiredType.IsArray))
      return 0;

 switch (Type.GetTypeCode(desiredType))
 {
      case 3:
      case 6:
      case 7:
      case 9:
      case 11:
      case 13:
      case 14:
      case 15:
          return 1;
 }
 ;return 0;
+9  A: 

You can find out if a variable is numeric using the Type.GetTypeCode() method:

TypeCode typeCode = Type.GetTypeCode(desiredType);

if (typeCode == TypeCode.Double || typeCode == TypeCode.Integer || ...)
     return true;

You'll need to complete all the available numeric types in the "..." part ;)

More details here: TypeCode Enumeration

Jon Limjap
I was hoping I wouldn't have to check every type, but this works thanks!
Nescio
FYI, this code will not work if they're nullable types. In that case, the typecode returns as Object. Still trying to figure out how to get around that. Anyone?
Codewerks
Better: Foreach (TypeCode typecode in new TypeCode[] { TypeCode.Double [...] } if typecode == Type.GetTypeCode(desiredType) return true; return false;
tsilb
+3  A: 

Great article here Exploring IsNumeric for C#.

Option 1:

Reference Microsoft.VisualBasic.dll, then do the following:

if (Microsoft.VisualBasic.Information.IsNumeric("5"))
{
 //Do Something
}

Option 2:

public static bool Isumeric (object Expression)
{
bool f;
ufloat64 a;
long l;

IConvertible iConvertible = null;
if ( ((Expression is IConvertible)))
{
   iConvertible = (IConvertible) Expression;
}

if (iConvertible == null)
{
   if ( ((Expression is char[])))
   {
       Expression = new String ((char[]) Expression);
       goto IL_002d; 'hopefully inserted by optimizer
   }
   return 0;
}
IL_002d:
TypeCode typeCode = iConvertible.GetTypeCode ();
if ((typeCode == 18) || (typeCode == 4))
{
    string str = iConvertible.ToString (null);
   try
   {
        if ( (StringType.IsHexOrOctValue (str, l)))
   {
        f = true;
        return f;
   }
}
catch (Exception )
{
    f = false;
    return f;
};
return DoubleType.TryParse (str, a);
}
return Utils.IsNumericTypeCode (typeCode);
}

internal static bool IsNumericType (Type typ)
{
bool f;
TypeCode typeCode;
if ( (typ.IsArray))
{
    return 0;
}
switch (Type.GetTypeCode (typ))
{
case 3: 
case 6: 
case 7: 
case 9: 
case 11: 
case 13: 
case 14: 
case 15: 
   return 1;
};
return 0;
}
smink
A: 

Use Type.IsValueType() and TryParse():

public bool IsInteger(Type t)
{
   int i;
   return t.IsValueType && int.TryParse(t.ToString(), out i);
}
Mike Post
"t" is not a variable of the Type in question. It *is* the type in question. t will never be a number in this code.
daughtkom
A: 

VB has an IsNumeric function; in C#, I use Int.TryParse.

tsilb
Int.TryParse will not work in this case. The OP specifically said the value is not known, only the Type.
Adam Glauser
A: 
''// Return true if a type is a numeric type.
Private Function IsNumericType(ByVal this As Type) As Boolean
    ''// All the numeric types have bits 11xx set whereas non numeric do not.
    ''// That is if you include char type which is 4(decimal) = 100(binary).
    If this.IsArray Then Return False
    If (Type.GetTypeCode(this) And &HC) > 0 Then Return True
    Return False
End Function
Mike