SmartPascal
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
  ShowMessage('##### : '+FormatFloat('#####', float));
  ShowMessage('00000 : '+FormatFloat('00000', float));
  ShowMessage('0     : '+FormatFloat('0'    , float));
  ShowMessage('#,##0 : '+FormatFloat('#,##0', float));
  ShowMessage(',0    : '+FormatFloat(',0'   , float));
  ShowMessage('');

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

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

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

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

  // Different format for zero value
  ShowMessage('0.0 : '+FormatFloat('0.0' , 0.0));
  ShowMessage('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