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 the same way as the ANSI C
17       sprintf procedure (it uses sprintf  in  its  implementation).   Format‐
18       String  indicates  how  to format the result, using % conversion speci‐
19       fiers as in sprintf, and the additional arguments, if any, provide val‐
20       ues to be substituted into the result.  The return value from format is
21       the formatted string.
22

DETAILS ON FORMATTING

24       The command operates by scanning formatString from left to right.  Each
25       character  from  the  format  string  is  appended to the result string
26       unless it is a percent sign.  If the character is a % then  it  is  not
27       copied  to  the result string.  Instead, the characters following the %
28       character are treated as a conversion specifier.  The conversion speci‐
29       fier controls the conversion of the next successive arg to a particular
30       format and the result is appended to the result string in place of  the
31       conversion  specifier.   If there are multiple conversion specifiers in
32       the format string, then each one controls the conversion of  one  addi‐
33       tional  arg.   The format command must be given enough args to meet the
34       needs of all of the conversion specifiers in formatString.
35
36       Each conversion specifier may contain up to  six  different  parts:  an
37       XPG3  position specifier, a set of flags, a minimum field width, a pre‐
38       cision, a length modifier, and a conversion character.   Any  of  these
39       fields  may be omitted except for the conversion character.  The fields
40       that are present must appear in the order given above.  The  paragraphs
41       below discuss each of these fields in turn.
42
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       The  second  portion  of  a conversion specifier may contain any of the
54       following flag characters, in any order:
55
56       -         Specifies that the converted argument should  be  left-justi‐
57                 fied  in its field (numbers are normally right-justified with
58                 leading spaces if needed).
59
60       +         Specifies that a number should always be printed with a sign,
61                 even if positive.
62
63       space     Specifies  that  a  space should be added to the beginning of
64                 the number if the first character isn't a sign.
65
66       0         Specifies that the number should be padded on the  left  with
67                 zeroes instead of spaces.
68
69       #         Requests an alternate output form. For o and O conversions it
70                 guarantees that the first digit is always 0.  For x or X con‐
71                 versions, 0x or 0X (respectively) will be added to the begin‐
72                 ning of the result unless it is zero.  For all floating-point
73                 conversions (e, E, f, g, and G) it guarantees that the result
74                 always has a decimal point.  For g and G conversions it spec‐
75                 ifies that trailing zeroes should not be removed.
76
77       The  third portion of a conversion specifier is a number giving a mini‐
78       mum field width for this conversion.  It is typically used to make col‐
79       umns  line up in tabular printouts.  If the converted argument contains
80       fewer characters than the minimum field width then it will be padded so
81       that it is as wide as the minimum field width.  Padding normally occurs
82       by adding extra spaces on the left of the converted argument, but the 0
83       and  -  flags may be used to specify padding with zeroes on the left or
84       with spaces on the right, respectively.  If the minimum field width  is
85       specified as * rather than a number, then the next argument to the for‐
86       mat command determines the minimum field width; it must  be  a  numeric
87       string.
88
89       The fourth portion of a conversion specifier is a precision, which con‐
90       sists of a period followed by a number.  The number is used in  differ‐
91       ent  ways  for  different  conversions.  For e, E, and f conversions it
92       specifies the number of digits to appear to the right  of  the  decimal
93       point.  For g and G conversions it specifies the total number of digits
94       to appear, including those on both sides of the decimal point (however,
95       trailing  zeroes  after  the decimal point will still be omitted unless
96       the # flag has been specified).  For integer conversions, it  specifies
97       a  minimum  number  of digits to print (leading zeroes will be added if
98       necessary).  For s conversions it specifies the maximum number of char‐
99       acters to be printed; if the string is longer than this then the trail‐
100       ing characters will be dropped.  If the precision is specified  with  *
101       rather  than  a  number  then  the  next argument to the format command
102       determines the precision; it must be a numeric string.
103
104       The fifth part of a conversion specifier is a  length  modifier,  which
105       must  be h or l.  If it is h it specifies that the numeric value should
106       be truncated to a 16-bit  value  before  converting.   This  option  is
107       rarely  useful.   If it is l it specifies that the numeric value should │
108       be (at least) a 64-bit value.  If neither h nor l are present,  numeric │
109       values  are  interpreted  as  being  values  of the width of the native │
110       machine word, as described by tcl_platform(wordSize).
111
112       The last thing in a conversion specifier  is  an  alphabetic  character
113       that determines what kind of conversion to perform.  The following con‐
114       version characters are currently supported:
115
116       d         Convert integer to signed decimal string.
117
118       u         Convert integer to unsigned decimal string.
119
120       i         Convert integer to signed decimal string;   the  integer  may
121                 either be in decimal, in octal (with a leading 0) or in hexa‐
122                 decimal (with a leading 0x).
123
124       o         Convert integer to unsigned octal string.
125
126       x or X    Convert integer to unsigned hexadecimal string, using  digits
127                 ``0123456789abcdef'' for x and ``0123456789ABCDEF'' for X).   │
128
129       c                                                                       
130                 Convert integer to the Unicode character it represents.
131
132       s         No conversion; just insert string.
133
134       f         Convert floating-point number to signed decimal string of the
135                 form xx.yyy, where the number of y's  is  determined  by  the
136                 precision  (default: 6).  If the precision is 0 then no deci‐
137                 mal point is output.
138
139       e or e    Convert floating-point number to scientific notation  in  the
140                 form  x.yyyzz, where the number of y's is determined by the
141                 precision (default: 6).  If the precision is 0 then no  deci‐
142                 mal point is output.  If the E form is used then E is printed
143                 instead of e.
144
145       g or G    If the exponent is less than -4 or greater than or  equal  to
146                 the  precision,  then convert floating-point number as for %e
147                 or %E.  Otherwise convert as for %f.  Trailing zeroes  and  a
148                 trailing decimal point are omitted.
149
150       %         No conversion: just insert %.
151
152       For  the  numerical conversions the argument being converted must be an
153       integer or floating-point  string;  format  converts  the  argument  to
154       binary  and  then converts it back to a string according to the conver‐
155       sion specifier.
156

DIFFERENCES FROM ANSI SPRINTF

158       The behavior of the format command is the same as the  ANSI  C  sprintf
159       procedure except for the following differences:
160
161       [1]    %p and %n specifiers are not currently supported.
162
163       [2]    For  %c conversions the argument must be a decimal string, which
164              will then be converted to the corresponding character value.
165
166       [3]    The l modifier is ignored for real values and  on  64-bit  plat‐ │
167              forms,  which  are  always  converted  as if the l modifier were │
168              present (i.e. the types double and long are used for the  inter‐ │
169              nal  representation  of  real and integer values, respectively).
170              If the h modifier is specified then integer values are truncated
171              to  short before conversion.  Both h and l modifiers are ignored
172              on all other conversions.
173

EXAMPLES

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

SEE ALSO

216       scan(n), sprintf(3), string(n)
217
218

KEYWORDS

220       conversion specifier, format, sprintf, string, substitution
221
222
223
224Tcl                                   8.1                            format(n)
Impressum