Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

delphi

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.

like image 261
Re0sless Avatar asked Oct 27 '08 12:10

Re0sless


1 Answers

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.

like image 159
Alan Avatar answered Nov 08 '22 02:11

Alan