RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.FloatToStr Function

Converts a floating point value to a string.

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

FloatToStr converts the floating-point value given by Value to its string representation. The conversion uses general number format with 15 significant digits. 

For greater control over the formatting of the string, use the FloatToStrF function. 

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

If the given value is a NAN (not-a-number), the resulting string is 'NAN'. If the given value is positive infinity, the resulting string is 'INF'. If the given value is negative infinity, the resulting string is '-INF'.  

C++ Examples: 

 

/*
This example shows how to use the ArcTan and FloatToStr
functions to approximate Pi.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Extended ex = 16 * ArcTan(1.0/5.0) - 4.0 * ArcTan(1.0/239.0); // Machin's formula
  String mystring = FloatToStr(ex);
  Edit2->Text = mystring;
}

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  Edit1->Text = "3.14159265358979";
}

 

Delphi Examples: 

{
This example displays the compiler version number in the
text field.
}
procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := FloatToStr(System.CompilerVersion);
end;
{
This example shows how to use the ArcTan and FloatToStr
functions to approximate Pi.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  ex : Extended;
  mystring : String;
begin
  ex := 16 * ArcTan(1/5) - 4 * ArcTan(1/239); // Machin's formula
  mystring := FloatToStr(ex);
  Edit3.Text := mystring;
  Str(ex:25:23, mystring);
  Edit4.Text := mystring;
  mystring := FloatToStrF(ex, ffFixed, 35, 33);
  Edit2.Text := mystring;
  mystring := SysUtils.FormatFloat('#,##0.00000000000000000000000000;;Zero', ex);
  Edit5.Text := mystring;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  ex : Extended;
  myString : String;
begin
  ex := Pi;
//  mystring := FloatToStr(ex);
//  Str(ex:25:23, mystring);
  mystring := FloatToStrF(ex, ffFixed, 35, 33);
  Edit1.Text := mystring;
end;

 

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