C# - How to Determine Whether a Type Is a Number

C# - how to determine whether a Type is a number

Try this:

Type type = object.GetType();
bool isNumber = (type.IsPrimitiveImple && type != typeof(bool) && type != typeof(char));

The primitive types are Boolean, Byte,
SByte, Int16, UInt16, Int32, UInt32,
Int64, UInt64, Char, Double,and
Single.

Taking Guillaume's solution a little further:

public static bool IsNumericType(this object o)
{
switch (Type.GetTypeCode(o.GetType()))
{
case TypeCode.Byte:
case TypeCode.SByte:
case TypeCode.UInt16:
case TypeCode.UInt32:
case TypeCode.UInt64:
case TypeCode.Int16:
case TypeCode.Int32:
case TypeCode.Int64:
case TypeCode.Decimal:
case TypeCode.Double:
case TypeCode.Single:
return true;
default:
return false;
}
}

Usage:

int i = 32;
i.IsNumericType(); // True

string s = "Hello World";
s.IsNumericType(); // False

Identify if a string is a number

int n;
bool isNumeric = int.TryParse("123", out n);

Update As of C# 7:

var isNumeric = int.TryParse("123", out int n);

or if you don't need the number you can discard the out parameter

var isNumeric = int.TryParse("123", out _);

The var s can be replaced by their respective types!

Using .Net, how can I determine if a type is a Numeric ValueType?

A few years late here, but here's my solution (you can choose whether to include boolean). Solves for the Nullable case. XUnit test included

/// <summary>
/// Determines if a type is numeric. Nullable numeric types are considered numeric.
/// </summary>
/// <remarks>
/// Boolean is not considered numeric.
/// </remarks>
public static bool IsNumericType( Type type )
{
if (type == null)
{
return false;
}

switch (Type.GetTypeCode(type))
{
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;
case TypeCode.Object:
if ( type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
return IsNumericType(Nullable.GetUnderlyingType(type));
}
return false;
}
return false;
}

/// <summary>
/// Tests the IsNumericType method.
/// </summary>
[Fact]
public void IsNumericTypeTest()
{
// Non-numeric types
Assert.False(TypeHelper.IsNumericType(null));
Assert.False(TypeHelper.IsNumericType(typeof(object)));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull)));
Assert.False(TypeHelper.IsNumericType(typeof(bool)));
Assert.False(TypeHelper.IsNumericType(typeof(char)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime)));
Assert.False(TypeHelper.IsNumericType(typeof(string)));

// Arrays of numeric and non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(object[])));
Assert.False(TypeHelper.IsNumericType(typeof(DBNull[])));
Assert.False(TypeHelper.IsNumericType(typeof(bool[])));
Assert.False(TypeHelper.IsNumericType(typeof(char[])));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime[])));
Assert.False(TypeHelper.IsNumericType(typeof(string[])));
Assert.False(TypeHelper.IsNumericType(typeof(byte[])));
Assert.False(TypeHelper.IsNumericType(typeof(decimal[])));
Assert.False(TypeHelper.IsNumericType(typeof(double[])));
Assert.False(TypeHelper.IsNumericType(typeof(short[])));
Assert.False(TypeHelper.IsNumericType(typeof(int[])));
Assert.False(TypeHelper.IsNumericType(typeof(long[])));
Assert.False(TypeHelper.IsNumericType(typeof(sbyte[])));
Assert.False(TypeHelper.IsNumericType(typeof(float[])));
Assert.False(TypeHelper.IsNumericType(typeof(ushort[])));
Assert.False(TypeHelper.IsNumericType(typeof(uint[])));
Assert.False(TypeHelper.IsNumericType(typeof(ulong[])));

// numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal)));
Assert.True(TypeHelper.IsNumericType(typeof(double)));
Assert.True(TypeHelper.IsNumericType(typeof(short)));
Assert.True(TypeHelper.IsNumericType(typeof(int)));
Assert.True(TypeHelper.IsNumericType(typeof(long)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte)));
Assert.True(TypeHelper.IsNumericType(typeof(float)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort)));
Assert.True(TypeHelper.IsNumericType(typeof(uint)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong)));

// Nullable non-numeric types
Assert.False(TypeHelper.IsNumericType(typeof(bool?)));
Assert.False(TypeHelper.IsNumericType(typeof(char?)));
Assert.False(TypeHelper.IsNumericType(typeof(DateTime?)));

// Nullable numeric types
Assert.True(TypeHelper.IsNumericType(typeof(byte?)));
Assert.True(TypeHelper.IsNumericType(typeof(decimal?)));
Assert.True(TypeHelper.IsNumericType(typeof(double?)));
Assert.True(TypeHelper.IsNumericType(typeof(short?)));
Assert.True(TypeHelper.IsNumericType(typeof(int?)));
Assert.True(TypeHelper.IsNumericType(typeof(long?)));
Assert.True(TypeHelper.IsNumericType(typeof(sbyte?)));
Assert.True(TypeHelper.IsNumericType(typeof(float?)));
Assert.True(TypeHelper.IsNumericType(typeof(ushort?)));
Assert.True(TypeHelper.IsNumericType(typeof(uint?)));
Assert.True(TypeHelper.IsNumericType(typeof(ulong?)));

// Testing with GetType because of handling with non-numerics. See:
// http://msdn.microsoft.com/en-us/library/ms366789.aspx

// Using GetType - non-numeric
Assert.False(TypeHelper.IsNumericType((new object()).GetType()));
Assert.False(TypeHelper.IsNumericType(DBNull.Value.GetType()));
Assert.False(TypeHelper.IsNumericType(true.GetType()));
Assert.False(TypeHelper.IsNumericType('a'.GetType()));
Assert.False(TypeHelper.IsNumericType((new DateTime(2009, 1, 1)).GetType()));
Assert.False(TypeHelper.IsNumericType(string.Empty.GetType()));

// Using GetType - numeric types
// ReSharper disable RedundantCast
Assert.True(TypeHelper.IsNumericType((new byte()).GetType()));
Assert.True(TypeHelper.IsNumericType(43.2m.GetType()));
Assert.True(TypeHelper.IsNumericType(43.2d.GetType()));
Assert.True(TypeHelper.IsNumericType(((short)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((int)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((long)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((sbyte)2).GetType()));
Assert.True(TypeHelper.IsNumericType(2f.GetType()));
Assert.True(TypeHelper.IsNumericType(((ushort)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((uint)2).GetType()));
Assert.True(TypeHelper.IsNumericType(((ulong)2).GetType()));
// ReSharper restore RedundantCast

// Using GetType - nullable non-numeric types
bool? nullableBool = true;
Assert.False(TypeHelper.IsNumericType(nullableBool.GetType()));
char? nullableChar = ' ';
Assert.False(TypeHelper.IsNumericType(nullableChar.GetType()));
DateTime? nullableDateTime = new DateTime(2009, 1, 1);
Assert.False(TypeHelper.IsNumericType(nullableDateTime.GetType()));

// Using GetType - nullable numeric types
byte? nullableByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableByte.GetType()));
decimal? nullableDecimal = 12.2m;
Assert.True(TypeHelper.IsNumericType(nullableDecimal.GetType()));
double? nullableDouble = 12.32;
Assert.True(TypeHelper.IsNumericType(nullableDouble.GetType()));
short? nullableInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt16.GetType()));
short? nullableInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt32.GetType()));
short? nullableInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableInt64.GetType()));
sbyte? nullableSByte = 12;
Assert.True(TypeHelper.IsNumericType(nullableSByte.GetType()));
float? nullableSingle = 3.2f;
Assert.True(TypeHelper.IsNumericType(nullableSingle.GetType()));
ushort? nullableUInt16 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt16.GetType()));
ushort? nullableUInt32 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt32.GetType()));
ushort? nullableUInt64 = 12;
Assert.True(TypeHelper.IsNumericType(nullableUInt64.GetType()));
}

Checking if an object is a number

You will simply need to do a type check for each of the basic numeric types.

Here's an extension method that should do the job:

public static bool IsNumber(this object value)
{
return value is sbyte
|| value is byte
|| value is short
|| value is ushort
|| value is int
|| value is uint
|| value is long
|| value is ulong
|| value is float
|| value is double
|| value is decimal;
}

This should cover all numeric types.

Update

It seems you do actually want to parse the number from a string during deserialisation. In this case, it would probably just be best to use double.TryParse.

string value = "123.3";
double num;
if (!double.TryParse(value, out num))
throw new InvalidOperationException("Value is not a number.");

Of course, this wouldn't handle very large integers/long decimals, but if that is the case you just need to add additional calls to long.TryParse / decimal.TryParse / whatever else.

Type Checking: typeof, GetType, or is?

All are different.

  • typeof takes a type name (which you specify at compile time).
  • GetType gets the runtime type of an instance.
  • is returns true if an instance is in the inheritance tree.

Example

class Animal { } 
class Dog : Animal { }

void PrintTypes(Animal a) {
Console.WriteLine(a.GetType() == typeof(Animal)); // false
Console.WriteLine(a is Animal); // true
Console.WriteLine(a.GetType() == typeof(Dog)); // true
Console.WriteLine(a is Dog); // true
}

Dog spot = new Dog();
PrintTypes(spot);

What about typeof(T)? Is it also resolved at compile time?

Yes. T is always what the type of the expression is. Remember, a generic method is basically a whole bunch of methods with the appropriate type. Example:

string Foo<T>(T parameter) { return typeof(T).Name; }

Animal probably_a_dog = new Dog();
Dog definitely_a_dog = new Dog();

Foo(probably_a_dog); // this calls Foo<Animal> and returns "Animal"
Foo<Animal>(probably_a_dog); // this is exactly the same as above
Foo<Dog>(probably_a_dog); // !!! This will not compile. The parameter expects a Dog, you cannot pass in an Animal.

Foo(definitely_a_dog); // this calls Foo<Dog> and returns "Dog"
Foo<Dog>(definitely_a_dog); // this is exactly the same as above.
Foo<Animal>(definitely_a_dog); // this calls Foo<Animal> and returns "Animal".
Foo((Animal)definitely_a_dog); // this does the same as above, returns "Animal"

How can I check if a string is a number?

Look up double.TryParse() if you're talking about numbers like 1, -2 and 3.14159. Some others are suggesting int.TryParse(), but that will fail on decimals.

string candidate = "3.14159";
if (double.TryParse(candidate, out var parsedNumber))
{
// parsedNumber is a valid number!
}

EDIT: As Lukasz points out below, we should be mindful of the thread culture when parsing numbers with a decimal separator, i.e. do this to be safe:

double.TryParse(candidate, NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out var parsedNumber)

How to find out if a numeric type is signed or unsigned in C#

There aren't that many numeric types that are unsigned, so why not compose a list of that:

if (new Type[] { typeof(ushort), typeof(uint), typeof(ulong), typeof(byte) }.Contains(type))
{
// unsigned.
}

Or if you just want to compare the value (here o):

if (o is ushort || o is uint || o is ulong || o is byte)
{
// unsigned.
}

typeof() to check for Numeric values

There's nothing much to do, unfortunately. But from C# 3 onwards, you can do something fancier:

public static class NumericTypeExtension
{
public static bool IsNumeric(this Type dataType)
{
if (dataType == null)
throw new ArgumentNullException("dataType");

return (dataType == typeof(int)
|| dataType == typeof(double)
|| dataType == typeof(long)
|| dataType == typeof(short)
|| dataType == typeof(float)
|| dataType == typeof(Int16)
|| dataType == typeof(Int32)
|| dataType == typeof(Int64)
|| dataType == typeof(uint)
|| dataType == typeof(UInt16)
|| dataType == typeof(UInt32)
|| dataType == typeof(UInt64)
|| dataType == typeof(sbyte)
|| dataType == typeof(Single)
);
}
}

so your original code can be written like this:

if (!DC.DataType.IsNumeric())
{
MessageBox.Show("Non decimal data cant be calculated");
return;
}


Related Topics



Leave a reply



Submit