Sign in Join
FPDoc Onlineexperiment

StrToFloat function

In unit sysutils

Convert a string to a floating-point value.

Declaration

pascal
function StrToFloat(const S: string): 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

Example: ex90

pascal
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.

An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.