Sign in Join
FPDoc Onlineexperiment

TextToFloat function

In unit sysutils

Convert a buffer to a float value.

Declaration

pascal
function TextToFloat(Buffer: PChar; out Value: Extended): Boolean

Description

TextToFloat converts the string in Buffer to a floating point value. Buffer should contain a valid string representation of a floating point value (either in decimal or scientific notation).

If the buffer contains a decimal value, then the decimal separator character must be the value of the DecimalSeparator variable.

Note that this behaviour has changed, earlier implementations also allowed the use of '.' in addition to the decimal separator character.

The function returns True if the conversion was successful.

Errors

If there is an invalid character in the buffer, then the function returns False

See also

Example: ex91

pascal
Program Example91;

{ This program demonstrates the TextToFloat function }
{$mode objfpc}
{$h+ }

Uses SysUtils;

Const
  NrValues = 5;
  TestStr : Array[1..NrValues] of pchar =
           ('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]);
    If TextToFloat(TestStr[i],E) then
      Writeln('Converted value : ',E)
    else
      Writeln('Unable to convert value.');
    end;
end;

Begin
  DecimalSeparator:=',';
  Testit;
  DecimalSeparator:='.';
  Testit;
End.

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