ObjectPascal
FormatFloat
Function
Rich formatting of a floating point number into a string SysUtils unit
1  function FormatFloat ( const Formatting : string; Value : Extended ) : string;
2  function FormatFloat ( const Formatting : string; Value : Extended; FormatSettings : TFormatSettings ) : string;
Description
The FormatFloat function provides rich Formatting of a floating point number Value into a string.
 
The Formatting string may contain a mix of freeform text and control characters:
 
  : Forces digit display or 0
: Optional digit display
: Forces display of thousands
: Forces display of decimals
E+  : Forces signed exponent display
E-  : Optional sign exponent display
: Separator of +ve and -ve and zero values

 
These are best understood by looking at the sample code.
 
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.
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
FloatToStrF Convert a floating point value to a string with formatting
Format Rich formatting of numbers and text into a string
FormatCurr Rich formatting of a currency value into a string
FormatDateTime Rich formatting of a TDateTime variable into a string
NegCurrFormat Defines negative amount formatting in currency displays
StrToFloat Convert a number string into a floating point value
ThousandSeparator The character used to display the thousands separator
 
Example code : Showing all of the formatting data types
var
  float : extended;

begin
  // Set up our floating point number
  float := 1234.567;

  // Display a sample value using all of the format options

  // Round out the decimal value
  WriteLn('##### : '+FormatFloat('#####', float));
  WriteLn('00000 : '+FormatFloat('00000', float));
  WriteLn('0     : '+FormatFloat('0'    , float));
  WriteLn('#,##0 : '+FormatFloat('#,##0', float));
  WriteLn(',0    : '+FormatFloat(',0'   , float));
  WriteLn('');

  // Include the decimal value
  WriteLn('0.#### : '+FormatFloat('0.####', float));
  WriteLn('0.0000 : '+FormatFloat('0.0000', float));
  WriteLn('');

  // Scientific format
  WriteLn('0.0000000E+00 : '+FormatFloat('0.0000000E+00', float));
  WriteLn('0.0000000E-00 : '+FormatFloat('0.0000000E-00', float));
  WriteLn('#.#######E-## : '+FormatFloat('#.#######E-##', float));
  WriteLn('');

  // Include freeform text
  WriteLn('"Value = "0.0 : '+FormatFloat('"Value = "0.0', float));
  WriteLn('');

  // Different formatting for negative numbers
  WriteLn('0.0 : '+FormatFloat('0.0'              , -1234.567));
  WriteLn('0.0 "CR";0.0 "DB" : '+
              FormatFloat('0.0 "CR";0.0 "DB"', -1234.567));
  WriteLn('0.0 "CR";0.0 "DB" : '+
              FormatFloat('0.0 "CR";0.0 "DB"',  1234.567));
  WriteLn('');

  // Different format for zero value
  WriteLn('0.0 : '+FormatFloat('0.0' , 0.0));
  WriteLn('0.0;-0.0;"Nothing" : '+
              FormatFloat('0.0;-0.0;"Nothing"', 0.0));
end;
Show full unit code
   ##### : 1235
   00000 : 01235
   0     : 1235
   #,##0 : 1,235
   ,0    : 1,235
  
   0.#### : 1234.567
   0.0000 : 1234.5670
  
   0.0000000E+00 : 1.2345670E+03
   0.0000000E-00 : 1.2345670E03
   #.#######E-## : 1.234567E3
  
   "Value = " : Value = 1234.6
  
   0.0 : -1234.6
   0.0 "CR";0.0 "DB" : 1234.6 DB
   0.0 "CR";0.0 "DB" : 1234.6 CR
  
   0.0 : 0.0
   0.0;-0.0;"Nothing" : Nothing