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

silvioprog silvioprog at gmail.com
Mon Nov 28 10:26:38 CET 2011


2011/11/28 Santiago A. <svaa at ciberpiula.net>:
[...]
> By the way, I don't have a delphi near to check it. Is tryStrTofloat a fpc
> function or it is also a delphi function? If there is such function in
> delphi, someone gave up searching help too soon.

After F1 on Delphi:
=============

 Delphi Object and Component Reference
 TryStrToFloat function

Converts a given string to a floating-point value with a boolean success code.

Unit

SysUtils

Category

floating point conversion routines

Delphi syntax:

function TryStrToFloat(const S: string; out Value: Extended): Boolean; overload;

function TryStrToFloat(const S: string; out Value: Double): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single): Boolean; overload;

function TryStrToFloat(const S: string; out Value: Extended; const
FormatSettings: TFormatSettings): Boolean; overload;

function TryStrToFloat(const S: string; out Value: Double; const
FormatSettings: TFormatSettings): Boolean; overload;
function TryStrToFloat(const S: string; out Value: Single; const
FormatSettings: TFormatSettings): Boolean; overload;

C++ syntax:

extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
Extended &Value);

extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, double &Value);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, float &Value);

extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
Extended &Value, const TFormatSettings FormatSettings);

extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S,
double &Value, const TFormatSettings FormatSettings);
extern PACKAGE bool __fastcall TryStrToFloat(const AnsiString S, float
&Value, const TFormatSettings FormatSettings);

Description

Use TryStrToFloat to convert a astring, S, to a floating-point value.
S must consist of an optional sign (+ or -), a string of digits with
an optional decimal point, and an optional mantissa. The mantissa
consists of 'E' or 'e' followed by an optional sign (+ or -) and a
whole number. Leading and trailing blanks are ignored.

The DecimalSeparator global variable defines the character that must
be used as a decimal point. Thousand separators and currency symbols
are not allowed in the string. If S doesn't contain a valid value,
TryStrToFloat returns Default.

TryStrToFloat places the result in Value, and returns true if the
conversion was successful, false otherwise.

The first three forms of TryStrToFloat are not thread-safe, because
they use localization information contained in global variables. The
second three forms of TryStrToFloat, which are thread-safe, refer to
localization information contained in the FormatSettings parameter.
Before calling the thread-safe forms of TryStrToFloat, you must
populate FormatSettings with localization information. To populate
FormatSettings with a set of default locale values, call
GetLocaleFormatSettings.

-- 
Silvio Clécio
===============================================
Blog - <silvioprog.com.br>
Twitter - <twitter.com/silvioprog>
Facebook - <facebook.com/silvioprog>
LazSolutions - <code.google.com/p/lazsolutions>
Lazarus-BR - <groups.google.com.br/group/lazarus-br?hl=pt-BR>
===============================================
   * Conheça nosso canal IRC sobre Lazarus: #lazarus-br *
===============================================




More information about the Lazarus mailing list