1

i need discover a type.

scenario:

I receive from DB a unkown type and want classificate if him is Numeric or Alphanumeric how i can do that?

eg:

public object GetUnkown(){....};

var x = GetUnkown();

x is numeric or alphanumeric?have only two these possibilities.

1
  • Your question makes surprisingly little sense. Please provide more detail.
    – SLaks
    Mar 29, 2011 at 3:19

3 Answers 3

1

To test if it can be parsed as an integer:

int xInt;


CultureInfo culture = new CultureInfo("en-US");    
bool isInteger = int.TryParse(x, NumberStyles.AllowThousands, culture, out xInt);

if (isInteger)
{

}
else
{
 string xString = x.ToString();
}
3
  • Ha ha ha! This will so plainly fail in even trivial scenarios. x="123.456" is a number too!
    – A.R.
    Apr 21, 2011 at 15:30
  • No, it's not. However, "123,456" is.
    – AgentFire
    Feb 7, 2012 at 10:08
  • Updated to include NumberStyles to parse the thousands operator. Decimals can also be included if required.
    – Russell
    Feb 7, 2012 at 20:27
1
int numeric;
if (int32.TryParse(value, out numeric))
{
  ... numeric processing 
}
else
{
  ... alpha numeric processing
} 
2
  • 1
    Can you please close that parenthesis? :) Mar 29, 2011 at 4:30
  • Please note the comment in Russell's response.
    – A.R.
    Apr 21, 2011 at 18:53
1

There are a couple of ways to do this. If you are getting different data types from the database, then you can do a type comparison like so;


  Type t = x.GetType();
  bool isNumeric t == typeof(sbyte) ||  
   t == typeof(byte) ||  
   t == typeof(short) ||  
   t == typeof(ushort) ||  
   t == typeof(int) ||  
   t == typeof(uint) ||  
   t == typeof(long) ||  
   t == typeof(ulong) ||  
   t == typeof(float) ||  
   t == typeof(double) ||  
   t == typeof(decimal);

This is exhaustive, but it will give you the correct answer.

If you are always getting a string from the database, then you can use some of the built-in parsing functions that will work MOST of the time.

// DO NOT USE 'int.TryParse()' as it will FAIL for any non-integer number, i.e. "123.456"

decimal d;
bool isNumeric = decimal.TryParse(x, out d);

Decimal has the widest range of numbers in .NET (of the built in types) so this will cover a lot of cases. However it can still fail if your 'number' lies outside of its range. For example, let's say that you have

string n = "5123123189461894481984885646158419999";  
decimal d;  
bool isNumeric = decimal.TryParse(n, out d);

Even though x represents a number, 'isNumeric' will come back as false because the number is outside of the range of the decimal type. Lucky for you, these cases are exceedingly rare, and so you won't have to resort to some other, more intense string parsing approach to tell if it is a number or not. (Which I am not going to cover at this time.)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.