1format(n) Tcl Built-In Commands format(n)
2
3
4
5______________________________________________________________________________
6
8 format - Format a string in the style of sprintf
9
11 format formatString ?arg arg ...?
12______________________________________________________________________________
13
14
16 This command generates a formatted string in a fashion similar to the
17 ANSI C sprintf procedure. FormatString indicates how to format the
18 result, using % conversion specifiers as in sprintf, and the additional
19 arguments, if any, provide values to be substituted into the result.
20 The return value from format is the formatted string.
21
23 The command operates by scanning formatString from left to right. Each
24 character from the format string is appended to the result string
25 unless it is a percent sign. If the character is a % then it is not
26 copied to the result string. Instead, the characters following the %
27 character are treated as a conversion specifier. The conversion speci‐
28 fier controls the conversion of the next successive arg to a particular
29 format and the result is appended to the result string in place of the
30 conversion specifier. If there are multiple conversion specifiers in
31 the format string, then each one controls the conversion of one addi‐
32 tional arg. The format command must be given enough args to meet the
33 needs of all of the conversion specifiers in formatString.
34
35 Each conversion specifier may contain up to six different parts: an
36 XPG3 position specifier, a set of flags, a minimum field width, a pre‐
37 cision, a size modifier, and a conversion character. Any of these
38 fields may be omitted except for the conversion character. The fields
39 that are present must appear in the order given above. The paragraphs
40 below discuss each of these fields in turn.
41
42 OPTIONAL POSITIONAL SPECIFIER
43 If the % is followed by a decimal number and a $, as in “%2$d”, then
44 the value to convert is not taken from the next sequential argument.
45 Instead, it is taken from the argument indicated by the number, where 1
46 corresponds to the first arg. If the conversion specifier requires
47 multiple arguments because of * characters in the specifier then suc‐
48 cessive arguments are used, starting with the argument given by the
49 number. This follows the XPG3 conventions for positional specifiers.
50 If there are any positional specifiers in formatString then all of the
51 specifiers must be positional.
52
53 OPTIONAL FLAGS
54 The second portion of a conversion specifier may contain any of the
55 following flag characters, in any order:
56
57 - Specifies that the converted argument should be left-justi‐
58 fied in its field (numbers are normally right-justified with
59 leading spaces if needed).
60
61 + Specifies that a number should always be printed with a sign,
62 even if positive.
63
64 space Specifies that a space should be added to the beginning of
65 the number if the first character is not a sign.
66
67 0 Specifies that the number should be padded on the left with
68 zeroes instead of spaces.
69
70 # Requests an alternate output form. For o conversions it guar‐
71 antees that the first digit is always 0. For x or X conver‐
72 sions, 0x or 0X (respectively) will be added to the beginning
73 of the result unless it is zero. For b conversions, 0b will
74 be added to the beginning of the result unless it is zero.
75 For all floating-point conversions (e, E, f, g, and G) it
76 guarantees that the result always has a decimal point. For g
77 and G conversions it specifies that trailing zeroes should
78 not be removed.
79
80 OPTIONAL FIELD WIDTH
81 The third portion of a conversion specifier is a decimal number giving
82 a minimum field width for this conversion. It is typically used to
83 make columns line up in tabular printouts. If the converted argument
84 contains fewer characters than the minimum field width then it will be
85 padded so that it is as wide as the minimum field width. Padding nor‐
86 mally occurs by adding extra spaces on the left of the converted argu‐
87 ment, but the 0 and - flags may be used to specify padding with zeroes
88 on the left or with spaces on the right, respectively. If the minimum
89 field width is specified as * rather than a number, then the next argu‐
90 ment to the format command determines the minimum field width; it must
91 be an integer value.
92
93 OPTIONAL PRECISION/BOUND
94 The fourth portion of a conversion specifier is a precision, which con‐
95 sists of a period followed by a number. The number is used in differ‐
96 ent ways for different conversions. For e, E, and f conversions it
97 specifies the number of digits to appear to the right of the decimal
98 point. For g and G conversions it specifies the total number of digits
99 to appear, including those on both sides of the decimal point (however,
100 trailing zeroes after the decimal point will still be omitted unless
101 the # flag has been specified). For integer conversions, it specifies
102 a minimum number of digits to print (leading zeroes will be added if
103 necessary). For s conversions it specifies the maximum number of char‐
104 acters to be printed; if the string is longer than this then the trail‐
105 ing characters will be dropped. If the precision is specified with *
106 rather than a number then the next argument to the format command
107 determines the precision; it must be a numeric string.
108
109 OPTIONAL SIZE MODIFIER
110 The fifth part of a conversion specifier is a size modifier, which must
111 be ll, h, or l. If it is ll it specifies that an integer value is
112 taken without truncation for conversion to a formatted substring. If
113 it is h it specifies that an integer value is truncated to a 16-bit
114 range before converting. This option is rarely useful. If it is l it
115 specifies that the integer value is truncated to the same range as that
116 produced by the wide() function of the expr command (at least a 64-bit
117 range). If neither h nor l are present, the integer value is truncated
118 to the same range as that produced by the int() function of the expr
119 command (at least a 32-bit range, but determined by the value of the
120 wordSize element of the tcl_platform array).
121
122 MANDATORY CONVERSION TYPE
123 The last thing in a conversion specifier is an alphabetic character
124 that determines what kind of conversion to perform. The following con‐
125 version characters are currently supported:
126
127 d Convert integer to signed decimal string.
128
129 u Convert integer to unsigned decimal string.
130
131 i Convert integer to signed decimal string (equivalent to d).
132
133 o Convert integer to unsigned octal string.
134
135 x or X Convert integer to unsigned hexadecimal string, using digits
136 “0123456789abcdef” for x and “0123456789ABCDEF” for X).
137
138 b Convert integer to unsigned binary string, using digits 0 and
139 1.
140
141 c Convert integer to the Unicode character it represents.
142
143 s No conversion; just insert string.
144
145 f Convert number to signed decimal string of the form xx.yyy,
146 where the number of y's is determined by the precision
147 (default: 6). If the precision is 0 then no decimal point is
148 output.
149
150 e or E Convert number to scientific notation in the form x.yyye±zz,
151 where the number of y's is determined by the precision
152 (default: 6). If the precision is 0 then no decimal point is
153 output. If the E form is used then E is printed instead of
154 e.
155
156 g or G If the exponent is less than -4 or greater than or equal to
157 the precision, then convert number as for %e or %E. Other‐
158 wise convert as for %f. Trailing zeroes and a trailing deci‐
159 mal point are omitted.
160
161 % No conversion: just insert %.
162
164 The behavior of the format command is the same as the ANSI C sprintf
165 procedure except for the following differences:
166
167 [1] Tcl guarantees that it will be working with UNICODE characters.
168
169 [2] %p and %n specifiers are not supported.
170
171 [3] For %c conversions the argument must be an integer value, which
172 will then be converted to the corresponding character value.
173
174 [4] The size modifiers are ignored when formatting floating-point
175 values. The ll modifier has no sprintf counterpart. The b
176 specifier has no sprintf counterpart.
177
179 Convert the numeric value of a UNICODE character to the character
180 itself:
181
182 set value 120
183 set char [format %c $value]
184
185 Convert the output of time into seconds to an accuracy of hundredths of
186 a second:
187
188 set us [lindex [time $someTclCode] 0]
189 puts [format "%.2f seconds to execute" [expr {$us / 1e6}]]
190
191 Create a packed X11 literal color specification:
192
193 # Each color-component should be in range (0..255)
194 set color [format "#%02x%02x%02x" $r $g $b]
195
196 Use XPG3 format codes to allow reordering of fields (a technique that
197 is often used in localized message catalogs; see msgcat) without
198 reordering the data values passed to format:
199
200 set fmt1 "Today, %d shares in %s were bought at $%.2f each"
201 puts [format $fmt1 123 "Global BigCorp" 19.37]
202
203 set fmt2 "Bought %2\$s equity ($%3$.2f x %1\$d) today"
204 puts [format $fmt2 123 "Global BigCorp" 19.37]
205
206 Print a small table of powers of three:
207
208 # Set up the column widths
209 set w1 5
210 set w2 10
211
212 # Make a nice header (with separator) for the table first
213 set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+
214 puts $sep
215 puts [format "| %-*s | %-*s |" $w1 "Index" $w2 "Power"]
216 puts $sep
217
218 # Print the contents of the table
219 set p 1
220 for {set i 0} {$i<=20} {incr i} {
221 puts [format "| %*d | %*ld |" $w1 $i $w2 $p]
222 set p [expr {wide($p) * 3}]
223 }
224
225 # Finish off by printing the separator again
226 puts $sep
227
229 scan(n), sprintf(3), string(n)
230
232 conversion specifier, format, sprintf, string, substitution
233
234
235
236Tcl 8.1 format(n)