Unit 'sysutils' Package
[Overview][Constants][Types][Classes][Procedures and functions][Variables][Index] [#rtl]

FormatFloat

Format a float according to a certain mask.

Declaration

Source position: sysstrh.inc line 241

function FormatFloat(

  const Format: string;

  Value: Extended

):string;

function FormatFloat(

  const Format: string;

  Value: Extended;

  const FormatSettings: TFormatSettings

):string;

Description

FormatFloat formats the floating-point value given by Value using the format specifications in Format. The format specifier can give format specifications for positive, negative or zero values (separated by a semicolon). Note that the minus sign must be explicitly specified in the section for negative numbers:

Writeln(FormatFloat('#0.00;#0.00-;0",FF));

If the format specifier is empty or the value needs more than 18 digits to be correctly represented, the result is formatted with a call to FloatToStrF with the ffGeneral format option.

The following format specifiers are supported:

0
is a digit place holder. If there is a corresponding digit in the value being formatted, then it replaces the 0. If not, the 0 is left as-is.
#
is also a digit place holder. If there is a corresponding digit in the value being formatted, then it replaces the #. If not, it is removed. by a space.
.
determines the location of the decimal point. Only the first '.' character is taken into account. If the value contains digits after the decimal point, then it is replaced by the value of the DecimalSeparator character.
,
determines the use of the thousand separator character in the output string. If the format string contains one or more ',' characters, then thousand separators will be used. The ThousandSeparator character is used.
E+
determines the use of scientific notation. If 'E+' or 'E-' (or their lowercase counterparts) are present then scientific notation is used. The number of digits in the output string is determined by the number of 0 characters after the 'E+'
;
This character separates sections for positive, negative, and zero numbers in the format string.

Errors

If an error occurs, an exception is raised.

See also

FloatToStr

  

Convert a float value to a string using a fixed format.

Example

Program Example89;

{ This program demonstrates the FormatFloat function }

Uses sysutils;

Const
  NrFormat=9;
  FormatStrings : Array[1..NrFormat] of string = (
        '',
        '0',
        '0.00',
        '#.##',
        '#,##0.00',
        '#,##0.00;(#,##0.00)',
        '#,##0.00;;Zero',
        '0.000E+00',
        '#.###E-0');
  NrValue = 5;
  FormatValues : Array[1..NrValue] of Double =
    (1234,-1234,0.5,0,-0.5);

  Width  = 12;
  FWidth = 20;

Var
  I,J : Integer;
  S : String;

begin
  Write('Format':FWidth);
  For I:=1 to NrValue do
    Write(FormatValues[i]:Width:2);
  Writeln;
  For I:=1 to NrFormat do
    begin
    Write(FormatStrings[i]:FWidth);
    For J:=1 to NrValue do
      begin
      S:=FormatFloat(FormatStrings[I],FormatValues[j]);
      Write(S:Width);
      end;
    Writeln;
    end;
End.

Documentation generated on: Dec 22 2024