IsNumeric() hell

As an old VB'er I have to ask - what's up with the lack of IsNumeric() in the base class library? Why is there a Char.IsNumber() but no string.IsNumeric()? Why should I have to reference Microsoft.VisualBasic.Dll for this “special” feature? (BTW - there's no harm in referencing that dll, it's a part of the framework just like System.XML is, but still...)

Here's a long and thorough discussion on the various “workarounds” needed to have this simple functionality implemented. Highlights:

  • Use the Microsoft.VisualBasic namespace(easy and clean, but why so far away?)
  • Convert the string to a char array and check each item using Char.IsNumber()
  • Convert the string to a char array and check each item using if (arr[i] > 57 || arr[i] < 48) return false;
  • Try a conversion and use a Try-Catch block to check for no- success(the worst performance option)
  • Use Double.TryParse() to return a boolean for success (now why don't we have this for the other types?)
  • (added later:) Yeah, you can also use a regular expression to check for a numeric.

Are we done yet?

This reminds me of a joke (which you surely know):

NASA spent $12 million for a space pen that can write upside down while the russians just used a pencil.

Published Monday, September 15, 2003 1:14 AM by RoyOsherove
Filed under:

Comments

Sunday, September 14, 2003 8:18 PM by Doug Ferguson

# re: IsNumeric() hell

In the .NET Framework you can do a lot of string operations with RegEx.

A Google search on "isnumeric regex" turned up an example at:

http://www.aspalliance.com/olson/methods/IsNumeric.aspx
Monday, September 15, 2003 12:34 PM by Xavier Lazard

# re: IsNumeric() hell

I was going to say that many of the methods listed in the discussion would not take into account numbers written in an exponential format. However the methods listed in Doug Ferguson's link would.
Monday, September 15, 2003 2:45 PM by Phil

# re: IsNumeric() hell

I just reference the MS.VB dll, or you can use Double.TryParse().
Monday, September 15, 2003 4:02 PM by Chad Osgood

# re: IsNumeric() hell

I'm Chad from the Tulsa DNUG (also the recently enigmatic blogger), so I extend my appreciation for any value you derived from the discussion :)
Monday, September 15, 2003 5:42 PM by Xavier Lazard

# re: IsNumeric() hell

a possible Hybrid implementation:

public bool IsNumeric(object s)
{
if ( s is decimal )
{
return true;
}
if ( s is int )
{
return true;
}
if ( s is short )
{
return true;
}
if ( s is long )
{
return true;
}
if ( s is uint )
{
return true;
}
if ( s is float )
{
return true;
}
if ( s is ulong )
{
return true;
}
if ( s is ushort )
{
return true;
}
if ( s is char )
{
return (((char)s > 57 || (char)s < 48));
}
if (s is string)
{
System.Double OutputValue;
return System.Double.TryParse((string) s,
NumberStyles.Any | NumberStyles.AllowHexSpecifier | NumberStyles.AllowHexSpecifier,
CultureInfo.CurrentCulture , out OutputValue );
}

return false;

}
Friday, June 25, 2004 7:12 AM by janine

# re: IsNumeric() hell

thanks a mill for highlighting this - wasn't life so much simpler back in those classic asp days!
Sunday, July 04, 2004 6:36 AM by TrackBack

# looking for isnumeric in vb.net?

i was wondering why a vb.net class file was throwing up errors when i called the isnumeric function the other day. after some googling i found out from roy osherove's blog that you now need to import the microsoft.visualbasic to...