Description |
The StrToFloat function converts a number string, FloatString such as '123.456' into an Extended floating point number.
It supports integer, floating point, and scientific (exponent) formats.
If a decimal point appears in FloatString, then it must match the current DecimalSeparator value.
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.
|
|
Notes |
The EConvertError exception is thrown if there are errors in FloatString, such as trailing blanks or invalid decimal characters.
|
|
Related commands |
Extended |
|
The floating point type with the highest capacity and precision |
FloatToStr |
|
Convert a floating point value to a string |
FloatToStrF |
|
Convert a floating point value to a string with formatting |
TFormatSettings |
|
A record for holding locale values for thread-safe functions |
|
|
|
Example code : Converting a scientific format number string |
// Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses SysUtils, // Unit containing the StrToFloat command Forms, Dialogs; type TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // Include form definitions procedure TForm1.FormCreate(Sender: TObject); var
stringValue : string;
floatValue : Extended;
begin // Set up the source string containing a number representation
stringValue := '123.456E+002';
// Convert it to a floating point number
floatValue := StrToFloat(stringValue);
// And display the value
ShowMessage(stringValue+' = '+FloatToStr(floatValue));
end; end.
|
Hide full unit code |
123.456E+002 = 12345.6
|
|
Example code : Catching string conversion errors |
var
A : Extended;
begin // We will catch conversion errors
try A := StrToFloat('10 E 2'); // Middle blanks are not supported
except
on Exception : EConvertError do
ShowMessage(Exception.Message);
end;
try A := StrToFloat('$FF'); // Hexadecimal values are not supported
except
on Exception : EConvertError do
ShowMessage(Exception.Message);
end;
end;
|
Show full unit code |
'10 E 2' is not a valid floating point value
'$FF' is not a valid floating point value
|
|