1format(n)                    Tcl Built-In Commands                   format(n)
2
3
4
5______________________________________________________________________________
6

NAME

8       format - Format a string in the style of sprintf
9

SYNOPSIS

11       format formatString ?arg arg ...?
12_________________________________________________________________
13
14

INTRODUCTION

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

DETAILS ON FORMATTING

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       If the % is followed by a decimal number and a $, as  in  “%2$d”,  then
43       the  value  to  convert is not taken from the next sequential argument.
44       Instead, it is taken from the argument indicated by the number, where 1
45       corresponds  to  the  first  arg.  If the conversion specifier requires
46       multiple arguments because of * characters in the specifier  then  suc‐
47       cessive  arguments  are  used,  starting with the argument given by the
48       number.  This follows the XPG3 conventions for  positional  specifiers.
49       If  there are any positional specifiers in formatString then all of the
50       specifiers must be positional.
51
52       The second portion of a conversion specifier may  contain  any  of  the
53       following flag characters, in any order:
54
55       -         Specifies  that  the converted argument should be left-justi‐
56                 fied in its field (numbers are normally right-justified  with
57                 leading spaces if needed).
58
59       +         Specifies that a number should always be printed with a sign,
60                 even if positive.
61
62       space     Specifies that a space should be added to  the  beginning  of
63                 the number if the first character is not a sign.
64
65       0         Specifies  that  the number should be padded on the left with
66                 zeroes instead of spaces.
67
68       #         Requests an alternate output form. For o and O conversions it
69                 guarantees that the first digit is always 0.  For x or X con‐
70                 versions, 0x or 0X (respectively) will be added to the begin‐
71                 ning of the result unless it is zero.  For all floating-point
72                 conversions (e, E, f, g, and G) it guarantees that the result
73                 always has a decimal point.  For g and G conversions it spec‐
74                 ifies that trailing zeroes should not be removed.
75
76       The third portion of a conversion specifier is a decimal number  giving
77       a  minimum  field  width  for this conversion.  It is typically used to
78       make columns line up in tabular printouts.  If the  converted  argument
79       contains  fewer characters than the minimum field width then it will be
80       padded so that it is as wide as the minimum field width.  Padding  nor‐
81       mally  occurs by adding extra spaces on the left of the converted argu‐
82       ment, but the 0 and - flags may be used to specify padding with  zeroes
83       on  the left or with spaces on the right, respectively.  If the minimum
84       field width is specified as * rather than a number, then the next argu‐
85       ment  to the format command determines the minimum field width; it must
86       be an integer value.
87
88       The fourth portion of a conversion specifier is a precision, which con‐
89       sists  of a period followed by a number.  The number is used in differ‐
90       ent ways for different conversions.  For e, E,  and  f  conversions  it
91       specifies  the  number  of digits to appear to the right of the decimal
92       point.  For g and G conversions it specifies the total number of digits
93       to appear, including those on both sides of the decimal point (however,
94       trailing zeroes after the decimal point will still  be  omitted  unless
95       the  # flag has been specified).  For integer conversions, it specifies
96       a minimum number of digits to print (leading zeroes will  be  added  if
97       necessary).  For s conversions it specifies the maximum number of char‐
98       acters to be printed; if the string is longer than this then the trail‐
99       ing  characters  will be dropped.  If the precision is specified with *
100       rather than a number then the  next  argument  to  the  format  command
101       determines the precision; it must be a numeric string.
102
103       The fifth part of a conversion specifier is a size modifier, which must
104       be ll, h, or l.  If it is ll it specifies  that  an  integer  value  is
105       taken  without  truncation for conversion to a formatted substring.  If
106       it is h it specifies that an integer value is  truncated  to  a  16-bit
107       range  before converting.  This option is rarely useful.  If it is l it
108       specifies that the integer value is truncated to the same range as that
109       produced  by the wide() function of the expr command (at least a 64-bit
110       range).  If neither h nor l are present, the integer value is truncated
111       to  the  same  range as that produced by the int() function of the expr
112       command (at least a 32-bit  range,  but  determined  by  the  value  of
113       tcl_platform(wordSize)).
114
115       The  last  thing  in  a conversion specifier is an alphabetic character
116       that determines what kind of conversion to perform.  The following con‐
117       version characters are currently supported:
118
119       d         Convert integer to signed decimal string.
120
121       u         Convert integer to unsigned decimal string.
122
123       i         Convert integer to signed decimal string (equivalent to d).
124
125       o         Convert integer to unsigned octal string.
126
127       x or X    Convert  integer to unsigned hexadecimal string, using digits
128                 “0123456789abcdef” for x and “0123456789ABCDEF” for X).
129
130       c         Convert integer to the Unicode character it represents.
131
132       s         No conversion; just insert string.
133
134       f         Convert number to signed decimal string of the  form  xx.yyy,
135                 where  the  number  of  y's  is  determined  by the precision
136                 (default: 6).  If the precision is 0 then no decimal point is
137                 output.
138
139       e or E    Convert  number to scientific notation in the form x.yyyzz,
140                 where the number  of  y's  is  determined  by  the  precision
141                 (default: 6).  If the precision is 0 then no decimal point is
142                 output.  If the E form is used then E is printed  instead  of
143                 e.
144
145       g or G    If  the  exponent is less than -4 or greater than or equal to
146                 the precision, then convert number as for %e or  %E.   Other‐
147                 wise convert as for %f.  Trailing zeroes and a trailing deci‐
148                 mal point are omitted.
149
150       %         No conversion: just insert %.
151

DIFFERENCES FROM ANSI SPRINTF

153       The behavior of the format command is the same as the  ANSI  C  sprintf
154       procedure except for the following differences:
155
156       [1]    %p and %n specifiers are not supported.
157
158       [2]    For  %c conversions the argument must be an integer value, which
159              will then be converted to the corresponding character value.
160
161       [3]    The size modifiers are ignored  when  formatting  floating-point
162              values.  The ll modifier has no sprintf counterpart.
163

EXAMPLES

165       Convert  the  numeric  value  of  a  UNICODE character to the character
166       itself:
167              set value 120
168              set char [format %c $value]
169
170       Convert the output of time into seconds to an accuracy of hundredths of
171       a second:
172              set us [lindex [time $someTclCode] 0]
173              puts [format "%.2f seconds to execute" [expr {$us / 1e6}]]
174
175       Create a packed X11 literal color specification:
176              # Each color-component should be in range (0..255)
177              set color [format "#%02x%02x%02x" $r $g $b]
178
179       Use  XPG3  format codes to allow reordering of fields (a technique that
180       is often used  in  localized  message  catalogs;  see  msgcat)  without
181       reordering the data values passed to format:
182              set fmt1 "Today, %d shares in %s were bought at $%.2f each"
183              puts [format $fmt1 123 "Global BigCorp" 19.37]
184
185              set fmt2 "Bought %2\$s equity ($%3$.2f x %1\$d) today"
186              puts [format $fmt2 123 "Global BigCorp" 19.37]
187
188       Print a small table of powers of three:
189              # Set up the column widths
190              set w1 5
191              set w2 10
192
193              # Make a nice header (with separator) for the table first
194              set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+
195              puts $sep
196              puts [format "| %-*s | %-*s |" $w1 "Index" $w2 "Power"]
197              puts $sep
198
199              # Print the contents of the table
200              set p 1
201              for {set i 0} {$i<=20} {incr i} {
202                 puts [format "| %*d | %*ld |" $w1 $i $w2 $p]
203                 set p [expr {wide($p) * 3}]
204              }
205
206              # Finish off by printing the separator again
207              puts $sep
208

SEE ALSO

210       scan(n), sprintf(3), string(n)
211

KEYWORDS

213       conversion specifier, format, sprintf, string, substitution
214
215
216
217Tcl                                   8.1                            format(n)
Impressum