Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4022162
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:26:28+00:00 2026-05-20T10:26:28+00:00

But here’s an example: Dim desiredType as Type if IsNumeric(desiredType) then … EDIT: I

  • 0

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;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T10:26:29+00:00Added an answer on May 20, 2026 at 10:26 am

    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()));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I dont know whether it is possible or not,But here is my question: I
My question is pretty vague :o) - But here is an example : When
This is often situation, but here is latest example: Companies have various contact data
I'm not sure if this is even possible but here goes: Currently I have
I'm not sure if token replace is the right phrase but here is what
I don't know if this is strictly a programming question but here goes. I
I'm not sure if I phrased that correctly but here's my dilemma. I inherited
Sorry i really didn't know how to phrase the question any better but here
I really didn't know how to phrase the question better but here goes: Lets
I don't know if this is possible in python but here goes ... I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.