Re: I Need an IsNumeric Method

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance

From: John Bowman (john.bowman_at_thermo.com)
Date: 02/27/04


Date: Fri, 27 Feb 2004 10:33:22 -0600

Jon,

Thanks for the help. I've whipped together a simple routine that may mostly
work for my purposes. It probably doesn't work in ALL cases, but for the
type of data I'll be encountering, I think it'll work... except for 1 thing.
I can't get it to work w/ scientific notation. I've included the code below:

The following calls return true correctly:
IsNumeric("1");
IsNumeric("1.0");
IsNumeric("5.12345678910123456");
IsNumeric("123456789");
IsNumeric("-1.2345");
IsNumeric("1-34bq55.5678");

The following calls return false incorrectly:
IsNumeric("1.234+e5");
IsNumeric("-1.234+e-5");
IsNumeric("5+e2");

public bool IsNumeric(string Str)
{
 bool bTmp = false;
 try
 {
  long lTmp = 0L;
  double dTmp = 0.0D;
  float fTmp = 0F;
  int nTmp = 0;

  bool bLong = false;
  bool bDouble = false;
  bool bFloat = false;
  bool bInt = false;

  try
  {
   //Try a long conversion.
   lTmp = Int64.Parse(Str);
   bLong = true;
  }
  catch(Exception eLong)
  {
   eLong = eLong;
  }

  try
  {
   //Try a double conversion.
   dTmp = Double.Parse(Str);
   bDouble = true;
  }
  catch(Exception eDouble)
  {
   eDouble = eDouble;
  }

  try
  {
   //Try a float conversion.
   fTmp = Single.Parse(Str);
   bFloat = true;
  }
  catch(Exception eFloat)
  {
   eFloat = eFloat;
  }

  try
  {
   //Try an int conversion.
   nTmp = Int32.Parse(Str);
   nTmp = Int16.Parse(Str);
   bInt = true;
  }
  catch(Exception eInt)
  {
   eInt = eInt;
  }

  //Were any conversions succesful?
  if((bLong == true) ||
    (bDouble == true) ||
    (bFloat == true) ||
    (bInt == true))
  {
   bTmp = true;
  }

 }
 catch(Exception eAll)
 {
  //Catch everything that goes wrong and return false;
  eAll = eAll;
 }

 return bTmp;
}

I also, tried using something like the following to get the scientific
notation to work:

dTmp = (double)Convert.ChangeType(Str, typeof(double));

but alas it didn't work either. Any ideas/thoughts?

TIA,

John

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1aa968e47d1dac3d98a28e@msnews.microsoft.com...
> <"John Bowman" <<Remove this before reply> john.bowman@thermo.com>>
> wrote:
> > Does anyone have a good/reliable approach to implementing an IsNumeric()
> > method that accepts a string that may represent a numerical value (eg.
such
> > as some text retrieved from an XML file)? Thus if you pass it "1" or
> > "5.12345" it returns true, but "1b3q" it returns false? I know VB has
this
> > sort of function, but was wondering how to implement something similar
in
> > C#. I suppose I could use the Convert methods and catch any exceptions
to
> > determine this, but is there a better technique?
>
> Simplest way: try to convert and catch the exception
> Next simplest way: use a regular expression to weed out some dodgy
> values, then try to convert and catch the exception
> Fastest way: write a hand-crafted routine to weed out some dodgy
> values, then try to convert and catch the exception
>
> It's actually not too difficult to write a routine to do this, but you
> need to be careful of a few things:
>
> o Do you want +5 to be okay?
> o How about -5?
> o How about whitespace?
> o Don't forget that .5 is generally considered okay, so don't
> require digits before dots
> o What about 1,5 in some European countries? What culture are you
> interested in?
>
> Generally, you should rule things out cautiously - it's okay to allow
> dodgy values as they'll be caught when you try the conversion, but it's
> not okay to reject any valid values, so err on the side of caution.
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too



Relevant Pages

  • Re: IWbemServices::ExecQuery Issue
    ... BOOL status = CreateWbemLocator; ... bool okay = true; ... // that contains the site code for the local site. ... Yogesh ...
    (microsoft.public.sms.misc)
  • Re: Python from Wise Guys Viewpoint
    ... The data conversion instructions were ... > not protected from causing an Operand Error, ... the conversion caused an exception because ... the Ariane-5 had a tilt angle beyond what the SRI was designed for. ...
    (comp.lang.python)
  • Re: Python from Wise Guys Viewpoint
    ... The data conversion instructions were ... > not protected from causing an Operand Error, ... the conversion caused an exception because ... the Ariane-5 had a tilt angle beyond what the SRI was designed for. ...
    (comp.lang.lisp)
  • Data source controls and parameter type conversion
    ... Exception I got: ... Since String and Int32 are incompatible types, ... perform conversion from String to Int32 in the first place? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: complex arithmetics
    ... That doesn't imply that l = int(r) is a 64-bit result, nor does it imply that l = intis a 64-bit result. ... All type conversion functions return default kind, ... The cmplx>>> fits the pattern <<< rather than being the exception. ... intrinsics, and, therefore, perhaps surprising. ...
    (comp.lang.fortran)