2 format - Format a string in the style of sprintf
5 format formatString ?arg arg ...?
9 This command generates a formatted string in the same way as the ANSI C
10 sprintf procedure (it uses sprintf in its implementation). Format-
11 String indicates how to format the result, using % conversion speci-
12 fiers as in sprintf, and the additional arguments, if any, provide val-
13 ues to be substituted into the result. The return value from format is
17 The command operates by scanning formatString from left to right. Each
18 character from the format string is appended to the result string
19 unless it is a percent sign. If the character is a % then it is not
20 copied to the result string. Instead, the characters following the %
21 character are treated as a conversion specifier. The conversion speci-
22 fier controls the conversion of the next successive arg to a particular
23 format and the result is appended to the result string in place of the
24 conversion specifier. If there are multiple conversion specifiers in
25 the format string, then each one controls the conversion of one addi-
26 tional arg. The format command must be given enough args to meet the
27 needs of all of the conversion specifiers in formatString.
29 Each conversion specifier may contain up to six different parts: an
30 XPG3 position specifier, a set of flags, a minimum field width, a pre-
31 cision, a length modifier, and a conversion character. Any of these
32 fields may be omitted except for the conversion character. The fields
33 that are present must appear in the order given above. The paragraphs
34 below discuss each of these fields in turn.
36 If the % is followed by a decimal number and a $, as in ``%2$d'', then
37 the value to convert is not taken from the next sequential argument.
38 Instead, it is taken from the argument indicated by the number, where 1
39 corresponds to the first arg. If the conversion specifier requires
40 multiple arguments because of * characters in the specifier then suc-
41 cessive arguments are used, starting with the argument given by the
42 number. This follows the XPG3 conventions for positional specifiers.
43 If there are any positional specifiers in formatString then all of the
44 specifiers must be positional.
46 The second portion of a conversion specifier may contain any of the
47 following flag characters, in any order:
49 - Specifies that the converted argument should be left-justi-
50 fied in its field (numbers are normally right-justified with
51 leading spaces if needed).
53 + Specifies that a number should always be printed with a sign,
56 space Specifies that a space should be added to the beginning of
57 the number if the first character isn't a sign.
59 0 Specifies that the number should be padded on the left with
60 zeroes instead of spaces.
62 # Requests an alternate output form. For o and O conversions it
63 guarantees that the first digit is always 0. For x or X con-
64 versions, 0x or 0X (respectively) will be added to the begin-
65 ning of the result unless it is zero. For all floating-point
66 conversions (e, E, f, g, and G) it guarantees that the result
67 always has a decimal point. For g and G conversions it spec-
68 ifies that trailing zeroes should not be removed.
70 The third portion of a conversion specifier is a number giving a mini-
71 mum field width for this conversion. It is typically used to make col-
72 umns line up in tabular printouts. If the converted argument contains
73 fewer characters than the minimum field width then it will be padded so
74 that it is as wide as the minimum field width. Padding normally occurs
75 by adding extra spaces on the left of the converted argument, but the 0
76 and - flags may be used to specify padding with zeroes on the left or
77 with spaces on the right, respectively. If the minimum field width is
78 specified as * rather than a number, then the next argument to the for-
79 mat command determines the minimum field width; it must be a numeric
82 The fourth portion of a conversion specifier is a precision, which con-
83 sists of a period followed by a number. The number is used in differ-
84 ent ways for different conversions. For e, E, and f conversions it
85 specifies the number of digits to appear to the right of the decimal
86 point. For g and G conversions it specifies the total number of digits
87 to appear, including those on both sides of the decimal point (however,
88 trailing zeroes after the decimal point will still be omitted unless
89 the # flag has been specified). For integer conversions, it specifies
90 a minimum number of digits to print (leading zeroes will be added if
91 necessary). For s conversions it specifies the maximum number of char-
92 acters to be printed; if the string is longer than this then the trail-
93 ing characters will be dropped. If the precision is specified with *
94 rather than a number then the next argument to the format command
95 determines the precision; it must be a numeric string.
97 The fifth part of a conversion specifier is a length modifier, which
98 must be h or l. If it is h it specifies that the numeric value should
99 be truncated to a 16-bit value before converting. This option is
100 rarely useful. If it is l it specifies that the numeric value should
101 be (at least) a 64-bit value. If neither h nor l are present, numeric
102 values are interpreted as being values of the width of the native
103 machine word, as described by tcl_platform(wordSize).
105 The last thing in a conversion specifier is an alphabetic character
106 that determines what kind of conversion to perform. The following con-
107 version characters are currently supported:
109 d Convert integer to signed decimal string.
111 u Convert integer to unsigned decimal string.
113 i Convert integer to signed decimal string; the integer may
114 either be in decimal, in octal (with a leading 0) or in hexa-
115 decimal (with a leading 0x).
117 o Convert integer to unsigned octal string.
119 x or X Convert integer to unsigned hexadecimal string, using digits
120 ``0123456789abcdef'' for x and ``0123456789ABCDEF'' for X).
122 c Convert integer to the Unicode character it represents.
124 s No conversion; just insert string.
126 f Convert floating-point number to signed decimal string of the
127 form xx.yyy, where the number of y's is determined by the
128 precision (default: 6). If the precision is 0 then no deci-
131 e or e Convert floating-point number to scientific notation in the
132 form x.yyye+-zz, where the number of y's is determined by the
133 precision (default: 6). If the precision is 0 then no deci-
134 mal point is output. If the E form is used then E is printed
137 g or G If the exponent is less than -4 or greater than or equal to
138 the precision, then convert floating-point number as for %e
139 or %E. Otherwise convert as for %f. Trailing zeroes and a
140 trailing decimal point are omitted.
142 % No conversion: just insert %.
144 For the numerical conversions the argument being converted must be an
145 integer or floating-point string; format converts the argument to
146 binary and then converts it back to a string according to the conver-
149 DIFFERENCES FROM ANSI SPRINTF
150 The behavior of the format command is the same as the ANSI C sprintf
151 procedure except for the following differences:
153 [1] %p and %n specifiers are not currently supported.
155 [2] For %c conversions the argument must be a decimal string, which
156 will then be converted to the corresponding character value.
158 [3] The l modifier is ignored for real values and on 64-bit plat-
159 forms, which are always converted as if the l modifier were
160 present (i.e. the types double and long are used for the inter-
161 nal representation of real and integer values, respectively).
162 If the h modifier is specified then integer values are truncated
163 to short before conversion. Both h and l modifiers are ignored
164 on all other conversions.
167 Convert the output of time into seconds to an accuracy of hundredths of
169 set us [lindex [time $someTclCode] 0]
170 puts [format "%.2f seconds to execute" [expr {$us / 1e6}]]
172 Create a packed X11 literal color specification:
173 # Each color-component should be in range (0..255)
174 set color [format "#%02x%02x%02x" $r $g $b]
176 Use XPG3 format codes to allow reordering of fields (a technique that
177 is often used in localized message catalogs; see msgcat) without
178 reordering the data values passed to format:
179 set fmt1 "Today, %d shares in %s were bought at $%.2f each"
180 puts [format $fmt1 123 "Global BigCorp" 19.37]
182 set fmt2 "Bought %2\$s equity ($%3$.2f x %1\$d) today"
183 puts [format $fmt2 123 "Global BigCorp" 19.37]
185 Print a small table of powers of three:
186 # Set up the column widths
190 # Make a nice header (with separator) for the table first
191 set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+
193 puts [format "| %-*s | %-*s |" $w1 "Index" $w2 "Power"]
196 # Print the contents of the table
198 for {set i 0} {$i<=20} {incr i} {
199 puts [format "| %*d | %*ld |" $w1 $i $w2 $p]
200 set p [expr {wide($p) * 3}]
203 # Finish off by printing the separator again
208 scan(n), sprintf(3), string(n)