StrToFloat

Convert a string to a floating-point value.

Declaration

Source position: sysstrh.inc line 196

  function StrToFloat(const S: string) : Extended;
  function StrToFloat(const S: string; 
                     const FormatSettings: TFormatSettings) : Extended;

Description

StrToFloat converts the string S to a floating point value. S should contain a valid string representation of a floating point value (either in decimal or scientific notation). The thousandseparator character may however not be used.

Up to and including version 2.2.2 of the compiler, if the string contains a decimal value, then the decimal separator character can either be a '.' or the value of the DecimalSeparator variable.

As of version 2.3.1, the string may contain only the DecimalSeparator character. The dot ('.') can no longer be used instead of the DecimalSeparator.

Errors

If the string S doesn't contain a valid floating point string, then an exception will be raised.

See also

Name Description
FloatToStr Convert a float value to a string using a fixed format.
FormatFloat Format a float according to a certain mask.
StrToInt Convert a string to an integer value.
TextToFloat Convert a buffer to a float value.

Example

Program Example90;
{ This program demonstrates the StrToFloat function }
{$mode objfpc}
{$h+ }
Uses SysUtils;
Const
  NrValues = 5;
  TestStr : Array[1..NrValues] of string =
           ('1,1','-0,2','1,2E-4','0','1E4');
Procedure Testit;
Var
  I : Integer;
  E : Extended;
begin
  Writeln('Using DecimalSeparator : ',DecimalSeparator);
  For I:=1 to NrValues do
    begin
    Writeln('Converting : ',TestStr[i]);
    Try
      E:=StrToFloat(TestStr[i]);
      Writeln('Converted value : ',E);
    except
      On E : Exception do
        Writeln('Exception when converting : ',E.Message);
    end;
    end;
end;
Begin
  DecimalSeparator:=',';
  Testit;
  DecimalSeparator:='.';
  Testit;
End.