DelphiBasics
StrToFloat
Function
Convert a number string into a floating point value SysUtils unit
1 function StrToFloat(FloatString string):Extended;
2 function StrToFloat (FloatString string; const FormatSettings TFormatSettings:Extended;
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
ExtendedThe floating point type with the highest capacity and precision
FloatToStrConvert a floating point value to a string
FloatToStrFConvert a floating point value to a string with formatting
TFormatSettingsA record for holding locale values for thread-safe functions
 Download this web site as a Windows program.




 
Example code : Converting a scientific format number string
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;
Show 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
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page