tags:

views:

781

answers:

5

What is the correct way to check if a value is a date/number in Delphi?

I know other languages have functions like isDate and isNaN, but what is the Delphi equivalent? at the minute I have this

function isNumeric(s1:string):boolean;
begin   
   // will throw exception if its not a number
  // there must be a better way to do this!!
  try
     StrTofloat(s1);
       result :=  TRUE ;
     except
       result := FALSE;
      end;
end;

But throwing exceptions cant be good, and it makes debugging hard as I keep seeing the exception dialogue every time the code is called.

+10  A: 

For integers, you could use TryStrToInt to check and convert without throwing exceptions:

function TryStrToInt(const s: string; out i : integer): boolean;

I'm not absolutely sure there is a full equivalent for floats, though, so you might need to use StrToFloat() and accept the possibility of a TFormatException.

Alan
Yes there is also TryStrToDate and TryStrToFloat and even more. Check SysUtils.
Gamecat
Thanks, Gamecat. It's been a few years ;)
Alan
CodeGear docs are available on-line if you need more help.http://docs.codegear.com/
stukelly
+2  A: 

There's a family of functions like TryStrToFloat, TryStrToDateTime etc. which do that. By the way, StrToFloat and others use the "Try" versions internally before raising exceptions.

Romulo A. Ceccon
+2  A: 

Firstly StrToFloatDef function is a useful alternative here if you want to stay in the language as delivered out the box.

However your best option is to deploy the JEDI code libraries (http://www.delphi-jedi.org/) and use the StrIsNumber function from there.

JEDI is open source, highly useful in lots of ways, and pretty much a must anyway.

Cruachan
A: 

You CAN turn off the annoying exceptions you don't want by checking the "ignore this exception" box that pops up. Future exceptions will be then ignored for that exception class. To start asking again, just go to the Options|Debugger Options and uncheck the ones you are ignoring.

skamradt
I think this is phenomenally dangerous way to go - you always want to know about your exceptions unless there is a very good reason not too - and type conversion isn't one. The only time I've ever done that is with the MS OpenGl DLLs which throw a know exception and you sort of have too.
Cruachan
Er, it's pretty clear that skamradt was talking about the integrated debugger's notification of exceptions (note the reference to "Options|Debugger Options"). That just means that the debugger won't trap them, and they'll be handled by the application itself.
Argalatyr
Exactly. Very useful when you have an exception which is being generated by an API call that you are properly handling in code and its being called in a loop with many iterations. Having the debugger ignore it is so much better than wearing out your f9 key.
skamradt
+2  A: 

Catching exceptions is very slow. If you plan on using such a function repeatedly in rapid succession, such as when validating fields during a file import, it might be worth it to roll your own function that does some simple character level checking before falling into a try/except block. I've used something similar before with a huge performance increase when parsing large amounts of data that was not in the correct format.

function IsNumeric(aValue : string): boolean;
var
  i : integer;
begin
  result := false;
  for i := 1 to Length(aValue) do
    if (NOT (aValue[i] in ['0'..'9', '.', '-', '+', 'E', 'e'])) then
      exit;
  try
    StrToFloat(aValue);
    result := true;
  except
  end;
end;

Obviously this may not be perfect, and has the limitation of hard-coded values in it. Depends entirely on your needs, this was just something simple that worked well for an internal process.

Kroden