FormatFloat function
Format a float according to a certain mask.
Declaration
function FormatFloat(const Format: string; Value: Extended): stringDescription
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
DecimalSeparatorcharacter. - ,
- 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
ThousandSeparatorcharacter 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
0characters 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
Example: ex89
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.An experiment, built from development snapshots. Not the official Free Pascal or Lazarus documentation.
