Re: I Need an IsNumeric Method
From: John Bowman (john.bowman_at_thermo.com)
Date: 02/27/04
- Next message: Hayato Iriumi: "Re: Code Generation"
- Previous message: Phil Barber: "Condition calls"
- In reply to: Jon Skeet [C# MVP]: "Re: I Need an IsNumeric Method"
- Next in thread: Daniel Pratt: "Re: I Need an IsNumeric Method"
- Reply: Daniel Pratt: "Re: I Need an IsNumeric Method"
- Reply: Jon Skeet [C# MVP]: "Re: I Need an IsNumeric Method"
- Messages sorted by: [ date ] [ thread ]
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
- Next message: Hayato Iriumi: "Re: Code Generation"
- Previous message: Phil Barber: "Condition calls"
- In reply to: Jon Skeet [C# MVP]: "Re: I Need an IsNumeric Method"
- Next in thread: Daniel Pratt: "Re: I Need an IsNumeric Method"
- Reply: Daniel Pratt: "Re: I Need an IsNumeric Method"
- Reply: Jon Skeet [C# MVP]: "Re: I Need an IsNumeric Method"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|