NAME
format - Format a string in the style of sprintf
SYNOPSIS
ffoorrmmaatt formatString ?arg arg ...?
IINNTTRROODDUUCCTTIIOONNThis command generates a formatted string in the same way as the ANSI C
sspprriinnttff procedure (it uses sspprriinnttff in its implementation). Format-
String indicates how to format the result, using %% conversion speci-
fiers as in sspprriinnttff, and the additional arguments, if any, provide val-
ues to be substituted into the result. The return value from ffoorrmmaatt isthe formatted string.
DDEETTAAIILLSS OONN FFOORRMMAATTTTIINNGGThe command operates by scanning formatString from left to right. Each
character from the format string is appended to the result string
unless it is a percent sign. If the character is a %% then it is not
copied to the result string. Instead, the characters following the %%
character are treated as a conversion specifier. The conversion speci-
fier controls the conversion of the next successive arg to a particularformat and the result is appended to the result string in place of the
conversion specifier. If there are multiple conversion specifiers inthe format string, then each one controls the conversion of one addi-
tional arg. The ffoorrmmaatt command must be given enough args to meet theneeds of all of the conversion specifiers in formatString.
Each conversion specifier may contain up to six different parts: anXPG3 position specifier, a set of flags, a minimum field width, a pre-
cision, a length modifier, and a conversion character. Any of these fields may be omitted except for the conversion character. The fields that are present must appear in the order given above. The paragraphs below discuss each of these fields in turn.If the %% is followed by a decimal number and a $$, as in ``%%22$$dd'', then
the value to convert is not taken from the next sequential argument. Instead, it is taken from the argument indicated by the number, where 1 corresponds to the first arg. If the conversion specifier requiresmultiple arguments because of ** characters in the specifier then suc-
cessive arguments are used, starting with the argument given by the number. This follows the XPG3 conventions for positional specifiers.If there are any positional specifiers in formatString then all of the
specifiers must be positional. The second portion of a conversion specifier may contain any of the following flag characters, in any order:- Specifies that the converted argument should be left-justi-
fied in its field (numbers are normally right-justified with
leading spaces if needed). ++ Specifies that a number should always be printed with a sign, even if positive. space Specifies that a space should be added to the beginning of the number if the first character isn't a sign. 00 Specifies that the number should be padded on the left with zeroes instead of spaces.## Requests an alternate output form. For oo and OO conversions it
guarantees that the first digit is always 00. For xx or XX con-
versions, 00xx or 00XX (respectively) will be added to the begin-
ning of the result unless it is zero. For all floating-point
conversions (ee, EE, ff, gg, and GG) it guarantees that the resultalways has a decimal point. For gg and GG conversions it spec-
ifies that trailing zeroes should not be removed.The third portion of a conversion specifier is a number giving a mini-
mum field width for this conversion. It is typically used to make col-
umns line up in tabular printouts. If the converted argument contains fewer characters than the minimum field width then it will be padded so that it is as wide as the minimum field width. Padding normally occurs by adding extra spaces on the left of the converted argument, but the 00and - flags may be used to specify padding with zeroes on the left or
with spaces on the right, respectively. If the minimum field width isspecified as ** rather than a number, then the next argument to the ffoorr-
mmaatt command determines the minimum field width; it must be a numeric string.The fourth portion of a conversion specifier is a precision, which con-
sists of a period followed by a number. The number is used in differ-
ent ways for different conversions. For ee, EE, and ff conversions it specifies the number of digits to appear to the right of the decimal point. For gg and GG conversions it specifies the total number of digits to appear, including those on both sides of the decimal point (however, trailing zeroes after the decimal point will still be omitted unlessthe ## flag has been specified). For integer conversions, it specifies
a minimum number of digits to print (leading zeroes will be added ifnecessary). For ss conversions it specifies the maximum number of char-
acters to be printed; if the string is longer than this then the trail-
ing characters will be dropped. If the precision is specified with ** rather than a number then the next argument to the ffoorrmmaatt command determines the precision; it must be a numeric string. The fifth part of a conversion specifier is a length modifier, which must be hh or ll. If it is hh it specifies that the numeric value shouldbe truncated to a 16-bit value before converting. This option is
rarely useful. If it is ll it specifies that the numeric value should |be (at least) a 64-bit value. If neither hh or ll are present, numeric |
values are interpreted as being values of the width of the native | machine word, as described by ttccllppllaattffoorrmm((wwoorrddSSiizzee)). The last thing in a conversion specifier is an alphabetic characterthat determines what kind of conversion to perform. The following con-
version characters are currently supported: dd Convert integer to signed decimal string. uu Convert integer to unsigned decimal string. ii Convert integer to signed decimal string; the integer mayeither be in decimal, in octal (with a leading 00) or in hexa-
decimal (with a leading 00xx). oo Convert integer to unsigned octal string. xx or XX Convert integer to unsigned hexadecimal string, using digits ``0123456789abcdef'' for xx and ``0123456789ABCDEF'' for XX). | cc || Convert integer to the Unicode character it represents. ss No conversion; just insert string.ff Convert floating-point number to signed decimal string of the
form xx.yyy, where the number of y's is determined by theprecision (default: 6). If the precision is 0 then no deci-
mal point is output.ee or ee Convert floating-point number to scientific notation in the
form x.yyyee++-zz, where the number of y's is determined by the
precision (default: 6). If the precision is 0 then no deci-
mal point is output. If the EE form is used then EE is printed instead of ee.gg or GG If the exponent is less than -4 or greater than or equal to
the precision, then convert floating-point number as for %%ee
or %%EE. Otherwise convert as for %%ff. Trailing zeroes and a
trailing decimal point are omitted.%% No conversion: just insert %%.
For the numerical conversions the argument being converted must be aninteger or floating-point string; format converts the argument to
binary and then converts it back to a string according to the conver-
sion specifier. DDIIFFFFEERREENNCCEESS FFRROOMM AANNSSII SSPPRRIINNTTFFThe behavior of the format command is the same as the ANSI C sspprriinnttff
procedure except for the following differences:[1] %%pp and %%nn specifiers are not currently supported.
[2] For %%cc conversions the argument must be a decimal string, which
will then be converted to the corresponding character value.[3] The ll modifier is ignored for real values and on 64-bit plat- |
forms, which are always converted as if the ll modifier were |present (i.e. the types ddoouubbllee and lloonngg are used for the inter- |
nal representation of real and integer values, respectively). If the hh modifier is specified then integer values are truncated to sshhoorrtt before conversion. Both hh and ll modifiers are ignored on all other conversions.SEE ALSO
sprintf(3), string(n) KKEEYYWWOORRDDSSconversion specifier, format, sprintf, string, substitution
Tcl 8.1 format(n)