DelphiBasics
FloatToStrF
Function
Convert a floating point value to a string with formatting SysUtils unit
1  function FloatToStrF ( Value : Extended; Format : TFloatFormat; Precision, Digits : Integer ) : string;
2  function FloatToStrF ( Value : Extended; Format : TFloatFormat; Precision, Digits : Integer; const?FormatSettings : TFormatSettings ) : string;
Description
The FloatToStrF function converts a floating point number Value into a displayable string, with great control over the formatting via the Format, Precision, and Digits values.
 
The Value type may be any of the floating point types.
 
The Format parameter is defined by the TFloatFormat (SysUtils) type :
 
ffCurrency  eg : ?2,345.60
ffExponent  eg : 2.3456E+04
ffFixed  eg : 2345.60
ffGeneral  eg : 2345.6
ffNumber  eg : 2,345.6

 
The other parameters are dependent on this format. See TFloatFormat for full details.
 
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.
Notes
You can change the currency string from the default (such as '$' in the USA, '?' in the UK) using the CurrencyString variable.

You can change the position of the currency string using the CurrencyFormat variable.

You can change the decimal point value by setting the DecimalSeparator character.

You can change the thousands separator value by setting the ThousandSeparator character.

If the full number of digits before the decimal point (the mantissa) cannot be displayed, then the display reverts to the exponent (scientific) format.
Related commands
CurrencyDecimals Defines decimal digit count in the Format function
CurrencyFormat Defines currency string placement in curr display functions
CurrencyString The currency string used in currency display functions
DecimalSeparator The character used to display the decimal point
FloatToStr Convert a floating point value to a string
NegCurrFormat Defines negative amount formatting in currency displays
TFloatFormat Formats for use in floating point number display functions
ThousandSeparator The character used to display the thousands separator
 Author links

 Buy Website Traffic at
 Buywebsitetrafficexperts.com

 Buy Proxies at
 Buyproxies.io
 
 Download this web site as a Windows program.




 
Example code : Display numbers as financial values
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in a Currency format
  CurrencyString := '? ';
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffCurrency, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffCurrency, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffCurrency, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffCurrency, 2, 4));
end;
 
end.
Hide full unit code
   Using 8,4 = ? 1,234.5670
   Using 4,4 = ? 1,235.0000
   Using 4,2 = ? 1,235.00
   Using 2,4 = 1.2E0003
 
Example code : Display numbers with Scientific formatting
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in a Scientific format
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffExponent, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffExponent, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffExponent, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffExponent, 2, 4));
end;
 
end.
Hide full unit code
   Using 8,4 = 1.2345670E+0003
   Using 4,4 = 1.235E+0003
   Using 4,2 = 1.235E+03
   Using 2,4 = 1.2E+0003
 
Example code : General display of numbers
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the FloatToStrF command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

var
  amount1 : Extended;
begin
  amount1 := 1234.567;

  // Display in Fixed format
  ShowMessage('Fixed formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffFixed, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffFixed, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffFixed, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffFixed, 2, 4));

  // Display in General format
  ShowMessage('');
  ShowMessage('General formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffGeneral, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffgeneral, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffGeneral, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffGeneral, 2, 4));

  // Display in Number format
  ShowMessage('');
  ShowMessage('Number formatting :');
  ShowMessage('');
  ShowMessage('Using 8,4 = '+FloatToStrF(amount1, ffNumber, 8, 4));
  ShowMessage('Using 4,4 = '+FloatToStrF(amount1, ffNumber, 4, 4));
  ShowMessage('Using 4,2 = '+FloatToStrF(amount1, ffNumber, 4, 2));
  ShowMessage('Using 2,4 = '+FloatToStrF(amount1, ffNumber, 2, 4));
end;
 
end.
Hide full unit code
   Fixed formatting :
  
   Using 8,4 = 1234.5670
   Using 4,4 = 1235.0000
   Using 4,2 = 1235.00
   Using 2,4 = 1.2E0003
  
   General formatting :
  
   Using 8,4 = 1234.567
   Using 4,4 = 1235
   Using 4,2 = 1235
   Using 2,4 = 1.2E0003
  
   Number formatting :
  
   Using 8,4 = 1,234.5670
   Using 4,4 = 1,235.0000
   Using 4,2 = 1,235.00
   Using 2,4 = 1.2E0003
  
 
Delphi Programming Neil Moffatt 2002 - 2017. All rights reserved.  |  Home Page