RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrToFloat Function

Converts a given string to a floating-point value.

Pascal
function StrToFloat(const S: string): Extended; overload;
function StrToFloat(const S: string; const FormatSettings: TFormatSettings): Extended; overload;
C++
Extended StrToFloat(const AnsiString S);
Extended StrToFloat(const AnsiString S, const TFormatSettings FormatSettings);

Use StrToFloat to convert a string, S, to a floating-point value. S must consist of an optional sign (+ or -), a string of digits with an optional decimal point, and an optional mantissa. The mantissa consists of 'E' or 'e' followed by an optional sign (+ or -) and a whole number. Leading and trailing blanks are ignored. 

The DecimalSeparator global variable or its TFormatSettings equivalent defines the character that is used as a decimal point. Thousand separators and currency symbols are not allowed in the string. If S doesn't contain a valid value, StrToFloat raises an EConvertError exception.  

The first form of StrToFloat is not thread-safe, because it uses localization information contained in global variables. The second form of StrToFloat, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of StrToFloat, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call GetLocaleFormatSettings.  

C++ Examples: 

 

/*
The following example uses two buttons, a string grid, and
text edit on a form. When the conversion button is clicked,
the values across the top of the string grid are converted
using the formats along the left side.  Select a cell and
use the Alter Cell button and text edit to change the
values converted and the conversion formats.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  for (int x = 1; x < StringGrid1->ColCount; x++)
  {
    for (int y = 1; y < StringGrid1->RowCount; y++)
    {
      StringGrid1->Cells[x][y] = FormatFloat(
      StringGrid1->Cells[0][y], StrToFloat(StringGrid1->Cells[x][0]));
    }
  }
}

int currCellCol, currCellRow;

void __fastcall TForm1::StringGrid1MouseUp(TObject *Sender, TMouseButton Button,
      TShiftState Shift, int X, int Y)
{
  StringGrid1->MouseToCell(X, Y, currCellCol, currCellRow);
}

// Alter Cell
void __fastcall TForm1::Button2Click(TObject *Sender)
{
  StringGrid1->Cells[currCellCol][currCellRow] = Edit1->Text;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  StringGrid1->Cells[1][0] = "1234";
  StringGrid1->Cells[2][0] = "-1234";
  StringGrid1->Cells[3][0] = "0.5";
  StringGrid1->Cells[4][0] = "0";
  StringGrid1->Cells[0][1] = "";
  StringGrid1->Cells[0][2] = "0";
  StringGrid1->Cells[0][3] = "0.00";
  StringGrid1->Cells[0][4] = "#.##";
  StringGrid1->Cells[0][5] = "#,##0.00";
  StringGrid1->Cells[0][6] = "#,##0.00;(#,##0.00)";
  StringGrid1->Cells[0][7] = "#,##0.00;;Zero";
  StringGrid1->Cells[0][8] = "0.000E+00";
  StringGrid1->Cells[0][9] = "#.###E-0";
}

 

Delphi Examples: 

{
The following example uses two buttons, a string grid, and
text edit on a form. When the conversion button is clicked,
the values across the top of the string grid are converted
using the formats along the left side.  Select a cell and
use the Alter Cell button and text edit to change the
values converted and the conversion formats.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  X, Y, I: Integer;
begin
  for X := 1 to StringGrid1.ColCount - 1 do
  begin
    for Y := 0 to StringGrid1.RowCount - 1 do
    begin
      StringGrid1.Cells[X,Y] := SysUtils.FormatFloat(
        StringGrid1.Cells[0,Y], StrToFloat(StringGrid1.Cells[X,0]));
    end;
  end;

end;

var currCellCol, currCellRow: Integer;

// Alter Cell
procedure TForm1.Button2Click(Sender: TObject);
begin
  StringGrid1.Cells[currCellCol,currCellRow] := Edit1.Text;
end;

procedure TForm1.StringGrid1MouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  StringGrid1.MouseToCell(X, Y, &currCellCol, &currCellRow);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  StringGrid1.Cells[1,0] := '1234';
  StringGrid1.Cells[2,0] := '-1234';
  StringGrid1.Cells[3,0] := '0.5';
  StringGrid1.Cells[4,0] := '0';
  StringGrid1.Cells[0,1] := '';
  StringGrid1.Cells[0,2] := '0';
  StringGrid1.Cells[0,3] := '0.00';
  StringGrid1.Cells[0,4] := '#.##';
  StringGrid1.Cells[0,5] := '#,##0.00';
  StringGrid1.Cells[0,6] := '#,##0.00;(#,##0.00)';
  StringGrid1.Cells[0,7] := '#,##0.00;;Zero';
  StringGrid1.Cells[0,8] := '0.000E+00';
  StringGrid1.Cells[0,9] := '#.###E-0';
end;
{
This example uses a form with a combo box, two list boxes,
an edit control, and a button.  The user selects a
conversion family from the combo box, which causes the list
boxes to fill with the conversion types registered with that
family. When the user clicks the button, a message box
appears showing the value in the edit control converted from
the units selected in the first list box to the units
selected in the second list box.
Note:   If you have not registered your own conversion families,
include the stdconvs unit in your uses clause to use the
standard conversion families.
}

{
The following OnCreate event handler for the form initializes
the combo box and list boxes:
}

procedure TForm1.FormCreate(Sender: TObject);
var
  FamilyList: TConvFamilyArray;
  i: Integer;
begin
  GetConvFamilies(FamilyList);
  for i := 0 to (Length(FamilyList) - 1) do
    ComboBox1.Items.Add(ConvFamilyToDescription(FamilyList[i]));
  ComboBox1.ItemIndex := 0; { select the first item }
  ComboBox1Select(ComboBox1); {trigger the event to initialize lists}
end;

{
The following OnClick event handler for the button reads the
value entered in the edit box and converts it between the
measurement units selected in the list boxes:
}
procedure TForm1.Button1Click(Sender: TObject);
var
  newVal: Double;
  CurFamily: TConvFamily;
  FromType, ToType: TConvType;
begin
  DescriptionToConvFamily(ComboBox1.Items[ComboBox1.ItemIndex], CurFamily);
  DescriptionToConvType(CurFamily, ListBox1.Items[ListBox1.ItemIndex], FromType);
  DescriptionToConvType(CurFamily, ListBox2.Items[ListBox2.ItemIndex], ToType);
  newVal := Convert(StrToFloat(Edit1.Text), FromType, ToType);
  ShowMessage(Format('%g %s', [newVal, ConvTypeToDescription(ToType)]));
end;

{
The following OnSelect event handler for the combo box
initializes the list boxes with the registered conversion
types for the selected family:
}
procedure TForm1.ComboBox1Select(Sender: TObject);
var
  TypeList: TConvTypeArray;
  i: Integer;
  CurFamily: TConvFamily;Description:string;
beginDescription := ComboBox1.Items[ComboBox1.ItemIndex];
  if DescriptionToConvFamily(Description, CurFamily) then
  begin
    GetConvTypes(CurFamily, TypeList);
    ListBox1.Items.Clear;
    ListBox2.Items.Clear;
    for i := 0 to Length(TypeList) -1 do
      ListBox1.Items.Add(ConvTypeToDescription(TypeList[i]));
    ListBox2.Items := ListBox1.Items;
    ListBox1.ItemIndex := 0;
    ListBox2.ItemIndex := 0;
  end;
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!