System.SysUtils.Format
Delphi
function Format(const Format: string; const Args: array of const): string;
const AFormatSettings: TFormatSettings): string;
C++
extern DELPHI_PACKAGE System::UnicodeString __fastcall Format(const System::UnicodeString Format, const System::TVarRec *Args, const System::NativeInt Args_High)/* overload */;
Contents
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
function | public | System.SysUtils.pas System.SysUtils.hpp |
System.SysUtils | System.SysUtils |
Description
Returns a formatted string assembled from a format string and an array of arguments.
The Format function formats the series of arguments in an open (untyped) array.
Format
is the format string. For information on format strings, see Format Strings, described in this topic.
Args
is an array of arguments to apply to the format specifiers in Format. For C++, Args_High
specifies the index of the last element of Args
(one less than the number of elements).
Format returns the results of applying the arguments in Args
to the format string Format
.
Two syntax forms are given for Format. The first form of Format is not thread-safe, because it uses localization information contained in global variables. The second form of Format, which is thread-safe, refers to localization information contained in the AFormatSettings
parameter. Before calling the thread-safe form of Format, you must populate AFormatSettings
with localization information. To populate AFormatSettings
with a set of default locale values, call TFormatSettings.Create.
Format Strings
Format strings specify required formats to general-purpose formatting routines. Format strings passed to the string formatting routines contain two types of objects--literal characters and format specifiers. Literal characters are copied word for word to the resulting string. Format specifiers fetch arguments from the argument list and apply the formatting to them.
Format specifiers have the following form:
"%" [index ":"] ["-"] [width] ["." prec] type
A format specifier begins with a % character. After the percent sign come the following elements, in this order:
- An optional argument zero-offset index specifier (that is, the first item has index 0),
[index ":"]
. - An optional left justification indicator,
["-"]
. - An optional width specifier,
[width]
. - An optional precision specifier,
["." prec]
. - The conversion type character,
type
.
The following table summarizes the possible values for type:
Value | Meaning |
---|---|
|
Decimal. The argument must be an integer value. The value is converted to a string of decimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has less digits, the resulting string is left-padded with zeros. |
|
Unsigned decimal. Similar to |
|
Scientific. The argument must be a floating-point value. The value is converted to a string of the form "-d.ddd...E+ddd". The resulting string starts with a minus sign if the number is negative. One digit always precedes the decimal point. The total number of digits in the resulting string (including the one before the decimal point) is given by the precision specifier in the format string; a default precision of 15 is assumed if no precision specifier is present. The "E" exponent character in the resulting string is always followed by a plus or minus sign and at least three digits. |
|
Fixed. The argument must be a floating-point value. The value is converted to a string of the form "-ddd.ddd...". The resulting string starts with a minus sign if the number is negative. The number of digits after the decimal point is given by the precision specifier in the format string—a default of 2 decimal digits is assumed if no precision specifier is present. |
|
General. The argument must be a floating-point value. The value is converted to the shortest possible decimal string using fixed or scientific format. The number of significant digits in the resulting string is given by the precision specifier in the format string; a default precision of 15 is assumed if no precision specifier is present. Trailing zeros are removed from the resulting string, and a decimal point appears only if necessary. The resulting string uses the fixed-point format if the number of digits to the left of the decimal point in the value is less than or equal to the specified precision, and if the value is greater than or equal to 0.00001. Otherwise the resulting string uses scientific format. |
|
Number. The argument must be a floating-point value. The value is converted to a string of the form "-d,ddd,ddd.ddd...". The |
|
Money. The argument must be a floating-point value. The value is converted to a string that represents a currency amount. The conversion is controlled by the CurrencyString, CurrencyFormat, NegCurrFormat, ThousandSeparator, DecimalSeparator, and CurrencyDecimals global variables or their equivalent in a TFormatSettings data structure. If the format string contains a precision specifier, it overrides the value given by the CurrencyDecimals global variable or its TFormatSettings equivalent. |
|
Pointer. The argument must be a pointer value. The value is converted to an 8-character string that represents the pointer's value in hexadecimal (in x64, the value is converted to a 16-character string instead). |
|
String. The argument must be a character, a string, or a PChar value. The string or character is inserted in place of the format specifier. The precision specifier, if present in the format string, specifies the maximum length of the resulting string. If the argument is a string that is longer than this maximum, the string is truncated. |
|
Hexadecimal. The argument must be an integer value. The value is converted to a string of hexadecimal digits. If the format string contains a precision specifier, it indicates that the resulting string must contain at least the specified number of digits; if the value has fewer digits, the resulting string is left-padded with zeros. |
Conversion characters may be specified in uppercase as well as in lowercase; both produce the same results.
To display the character %
(that is, to display a literal %, not to begin a format specifier), use the sequence %%
. Example:
Writeln(Format('%d%%', [100])); // displays '100%'
For all floating-point formats, the actual characters used as decimal and thousand separators are obtained from the DecimalSeparator
and ThousandSeparator
global variables or their TFormatSettings equivalent.
Index, width, and precision specifiers can be specified directly, using a decimal digit string (for example "%10d"), or indirectly, using an asterisk character (for example "%*.*f"). When using an asterisk, the next argument in the argument list becomes the value that is actually used. Note that width is an integer value, while precision is an unsigned integer value. For example,
Format ('%*.*f', [8, 2, 123.456]);
is equivalent to:
Format ('%8.2f', [123.456]);
Similarly, in C++:
TVarRec args[3] = {8,2,123.456};
Format ("%*.*f", args, 2);
is equivalent to:
TVarRec args[1] = {123.456};
Format ("%8.2f", args, 0);
A width specifier sets the minimum field width for a conversion. If the resulting string is shorter than the minimum field width, it is padded with blanks to increase the field width. The default is to right-justify the result by adding blanks in front of the value, but if the format specifier contains a left-justification indicator (an "-" en dash character preceding the width specifier), the result is left-justified by adding blanks after the value.
An index specifier sets the current argument list index to the specified value. The index of the first argument in the argument list is 0. Using index specifiers, it is possible to format the same argument multiple times. For example, "Format('%d %d %0:d %1:d', [10, 20])" produces the string '10 20 10 20'.
Variadic Format
The Clang compilers now supports a variadic version of Format(), allowing both ARRAYOFCONST and variadic syntax.
Classic and Clang compilers:
Format ("Hello int=%d float=%f, str=%s",
ARRAYOFCONST((10, 2.3L, String("str"))));
Clang compiler:
Format ("Hello int=%d float=%f, str=%s",
10, 2.3L, String("str"));
Note that Format allows indexing into the parameters, so you can have a format string accessing parameters in a different order than passed, or accessing one parameter multiple times.
Classic and Clang compilers:
Format ("%1:d %1:u %2:u", ARRAYOFCONST((myint, myuint)));
Clang compiler:
Format ("%1:d %1:u %2:u", myint, myuint);