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   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 and O conversions it
71                 guarantees that the first digit is always 0.  For x or X con‐
72                 versions, 0x or 0X (respectively) will be added to the begin‐
73                 ning  of the result unless it is zero.  For b conversions, 0b
74                 will be added to the beginning of the  result  unless  it  is
75                 zero.  For all floating-point conversions (e, E, f, g, and G)
76                 it guarantees that the result always  has  a  decimal  point.
77                 For  g  and  G  conversions it specifies that trailing zeroes
78                 should 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 binary string, using digits 0 and 1.
139
140       c         Convert integer to the Unicode character it represents.
141
142       s         No conversion; just insert string.
143
144       f         Convert number to signed decimal string of the  form  xx.yyy,
145                 where  the  number  of  y's  is  determined  by the precision
146                 (default: 6).  If the precision is 0 then no decimal point is
147                 output.
148
149       e or E    Convert  number to scientific notation in the form x.yyyzz,
150                 where the number  of  y's  is  determined  by  the  precision
151                 (default: 6).  If the precision is 0 then no decimal point is
152                 output.  If the E form is used then E is printed  instead  of
153                 e.
154
155       g or G    If  the  exponent is less than -4 or greater than or equal to
156                 the precision, then convert number as for %e or  %E.   Other‐
157                 wise convert as for %f.  Trailing zeroes and a trailing deci‐
158                 mal point are omitted.
159
160       %         No conversion: just insert %.
161

DIFFERENCES FROM ANSI SPRINTF

163       The behavior of the format command is the same as the  ANSI  C  sprintf
164       procedure except for the following differences:
165
166       [1]    Tcl guarantees that it will be working with UNICODE characters.
167
168       [2]    %p and %n specifiers are not supported.
169
170       [3]    For  %c conversions the argument must be an integer value, which
171              will then be converted to the corresponding character value.
172
173       [4]    The size modifiers are ignored  when  formatting  floating-point
174              values.   The  ll  modifier  has  no sprintf counterpart.  The b
175              specifier has no sprintf counterpart.
176

EXAMPLES

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

SEE ALSO

228       scan(n), sprintf(3), string(n)
229

KEYWORDS

231       conversion specifier, format, sprintf, string, substitution
232
233
234
235Tcl                                   8.1                            format(n)
Impressum