[Lazarus] How to check if a string is a valid floating-point number.

Santiago A. svaa at ciberpiula.net
Fri Nov 25 11:38:15 CET 2011

El 25/11/2011 10:16, Kjow escribió:

What about the standard pascal function  "val"

function IsStrFloatNum(s: string): Boolean;
 val(s, dummyNumber, posError);

Time ago I came across a program written by someone that had to import
data from an ascii file, and epending upon the contents of the line,
select the kind of record to import.  The initial programmer used
exceptions. "I try this record, if it raises an exception then I try the
other". I Changed it to a simple initial check of contents. The initial
version took about 30 seconds, my version 3 seconds.

Lesson: Exceptions should be use to catch errors, not to common tests.

> Hi all,
> I need to know if a string is a valid floating-point number, so I
> tried this function:
> function IsStrFloatNum(s: string): Boolean;
> begin
>   try
>     // try to convert the string into a floatnumber
>     StrToFloat(s);
>     // if there is no error the result is true
>     Result := True;
>   except
>     // if there is any error the result is false
>     Result := False;
>   end;
> end;
> (From: http://www.swissdelphicenter.ch/torry/showcode.php?id=304 )
> Well, I get this error:
> Debugger Exception Notification
> project.exe raised exception class 'EConvertError' with message:
> "Cross" is an invalid float
> I used this function some month ago in this project and it worked
> without problems, but now (some month after) I re-tried to re-build
> and re-use this function and I get this error.
> Did I inadvertently change something?
> Thank you!
> Kjow
> --
> _______________________________________________
> Lazarus mailing list
> Lazarus at lists.lazarus.freepascal.org
> http://lists.lazarus.freepascal.org/mailman/listinfo/lazarus


santiago A.
svaa at ciberpiula.net

More information about the Lazarus mailing list