1 |
578 |
markom |
'\"
|
2 |
|
|
'\" Copyright (c) 1993 The Regents of the University of California.
|
3 |
|
|
'\" Copyright (c) 1994-1996 Sun Microsystems, Inc.
|
4 |
|
|
'\"
|
5 |
|
|
'\" See the file "license.terms" for information on usage and redistribution
|
6 |
|
|
'\" of this file, and for a DISCLAIMER OF ALL WARRANTIES.
|
7 |
|
|
'\"
|
8 |
|
|
'\" RCS: @(#) $Id: format.n,v 1.1.1.1 2002-01-16 10:25:24 markom Exp $
|
9 |
|
|
'\"
|
10 |
|
|
.so man.macros
|
11 |
|
|
.TH format n "" Tcl "Tcl Built-In Commands"
|
12 |
|
|
.BS
|
13 |
|
|
'\" Note: do not modify the .SH NAME line immediately below!
|
14 |
|
|
.SH NAME
|
15 |
|
|
format \- Format a string in the style of sprintf
|
16 |
|
|
.SH SYNOPSIS
|
17 |
|
|
\fBformat \fIformatString \fR?\fIarg arg ...\fR?
|
18 |
|
|
.BE
|
19 |
|
|
|
20 |
|
|
.SH INTRODUCTION
|
21 |
|
|
.PP
|
22 |
|
|
This command generates a formatted string in the same way as the
|
23 |
|
|
ANSI C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its
|
24 |
|
|
implementation).
|
25 |
|
|
\fIFormatString\fR indicates how to format the result, using
|
26 |
|
|
\fB%\fR conversion specifiers as in \fBsprintf\fR, and the additional
|
27 |
|
|
arguments, if any, provide values to be substituted into the result.
|
28 |
|
|
The return value from \fBformat\fR is the formatted string.
|
29 |
|
|
|
30 |
|
|
.SH "DETAILS ON FORMATTING"
|
31 |
|
|
.PP
|
32 |
|
|
The command operates by scanning \fIformatString\fR from left to right.
|
33 |
|
|
Each character from the format string is appended to the result
|
34 |
|
|
string unless it is a percent sign.
|
35 |
|
|
If the character is a \fB%\fR then it is not copied to the result string.
|
36 |
|
|
Instead, the characters following the \fB%\fR character are treated as
|
37 |
|
|
a conversion specifier.
|
38 |
|
|
The conversion specifier controls the conversion of the next successive
|
39 |
|
|
\fIarg\fR to a particular format and the result is appended to
|
40 |
|
|
the result string in place of the conversion specifier.
|
41 |
|
|
If there are multiple conversion specifiers in the format string,
|
42 |
|
|
then each one controls the conversion of one additional \fIarg\fR.
|
43 |
|
|
The \fBformat\fR command must be given enough \fIarg\fRs to meet the needs
|
44 |
|
|
of all of the conversion specifiers in \fIformatString\fR.
|
45 |
|
|
.PP
|
46 |
|
|
Each conversion specifier may contain up to six different parts:
|
47 |
|
|
an XPG3 position specifier,
|
48 |
|
|
a set of flags, a minimum field width, a precision, a length modifier,
|
49 |
|
|
and a conversion character.
|
50 |
|
|
Any of these fields may be omitted except for the conversion character.
|
51 |
|
|
The fields that are present must appear in the order given above.
|
52 |
|
|
The paragraphs below discuss each of these fields in turn.
|
53 |
|
|
.PP
|
54 |
|
|
If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in
|
55 |
|
|
``\fB%2$d\fR'', then the value to convert is not taken from the
|
56 |
|
|
next sequential argument.
|
57 |
|
|
Instead, it is taken from the argument indicated by the number,
|
58 |
|
|
where 1 corresponds to the first \fIarg\fR.
|
59 |
|
|
If the conversion specifier requires multiple arguments because
|
60 |
|
|
of \fB*\fR characters in the specifier then
|
61 |
|
|
successive arguments are used, starting with the argument
|
62 |
|
|
given by the number.
|
63 |
|
|
This follows the XPG3 conventions for positional specifiers.
|
64 |
|
|
If there are any positional specifiers in \fIformatString\fR
|
65 |
|
|
then all of the specifiers must be positional.
|
66 |
|
|
.PP
|
67 |
|
|
The second portion of a conversion specifier may contain any of the
|
68 |
|
|
following flag characters, in any order:
|
69 |
|
|
.TP 10
|
70 |
|
|
\fB\-\fR
|
71 |
|
|
Specifies that the converted argument should be left-justified
|
72 |
|
|
in its field (numbers are normally right-justified with leading
|
73 |
|
|
spaces if needed).
|
74 |
|
|
.TP 10
|
75 |
|
|
\fB+\fR
|
76 |
|
|
Specifies that a number should always be printed with a sign,
|
77 |
|
|
even if positive.
|
78 |
|
|
.TP 10
|
79 |
|
|
\fIspace\fR
|
80 |
|
|
Specifies that a space should be added to the beginning of the
|
81 |
|
|
number if the first character isn't a sign.
|
82 |
|
|
.TP 10
|
83 |
|
|
\fB0\fR
|
84 |
|
|
Specifies that the number should be padded on the left with
|
85 |
|
|
zeroes instead of spaces.
|
86 |
|
|
.TP 10
|
87 |
|
|
\fB#\fR
|
88 |
|
|
Requests an alternate output form. For \fBo\fR and \fBO\fR
|
89 |
|
|
conversions it guarantees that the first digit is always \fB0\fR.
|
90 |
|
|
For \fBx\fR or \fBX\fR conversions, \fB0x\fR or \fB0X\fR (respectively)
|
91 |
|
|
will be added to the beginning of the result unless it is zero.
|
92 |
|
|
For all floating-point conversions (\fBe\fR, \fBE\fR, \fBf\fR,
|
93 |
|
|
\fBg\fR, and \fBG\fR) it guarantees that the result always
|
94 |
|
|
has a decimal point.
|
95 |
|
|
For \fBg\fR and \fBG\fR conversions it specifies that
|
96 |
|
|
trailing zeroes should not be removed.
|
97 |
|
|
.PP
|
98 |
|
|
The third portion of a conversion specifier is a number giving a
|
99 |
|
|
minimum field width for this conversion.
|
100 |
|
|
It is typically used to make columns line up in tabular printouts.
|
101 |
|
|
If the converted argument contains fewer characters than the
|
102 |
|
|
minimum field width then it will be padded so that it is as wide
|
103 |
|
|
as the minimum field width.
|
104 |
|
|
Padding normally occurs by adding extra spaces on the left of the
|
105 |
|
|
converted argument, but the \fB0\fR and \fB\-\fR flags
|
106 |
|
|
may be used to specify padding with zeroes on the left or with
|
107 |
|
|
spaces on the right, respectively.
|
108 |
|
|
If the minimum field width is specified as \fB*\fR rather than
|
109 |
|
|
a number, then the next argument to the \fBformat\fR command
|
110 |
|
|
determines the minimum field width; it must be a numeric string.
|
111 |
|
|
.PP
|
112 |
|
|
The fourth portion of a conversion specifier is a precision,
|
113 |
|
|
which consists of a period followed by a number.
|
114 |
|
|
The number is used in different ways for different conversions.
|
115 |
|
|
For \fBe\fR, \fBE\fR, and \fBf\fR conversions it specifies the number
|
116 |
|
|
of digits to appear to the right of the decimal point.
|
117 |
|
|
For \fBg\fR and \fBG\fR conversions it specifies the total number
|
118 |
|
|
of digits to appear, including those on both sides of the decimal
|
119 |
|
|
point (however, trailing zeroes after the decimal point will still
|
120 |
|
|
be omitted unless the \fB#\fR flag has been specified).
|
121 |
|
|
For integer conversions, it specifies a minimum number of digits
|
122 |
|
|
to print (leading zeroes will be added if necessary).
|
123 |
|
|
For \fBs\fR conversions it specifies the maximum number of characters to be
|
124 |
|
|
printed; if the string is longer than this then the trailing characters will be dropped.
|
125 |
|
|
If the precision is specified with \fB*\fR rather than a number
|
126 |
|
|
then the next argument to the \fBformat\fR command determines the precision;
|
127 |
|
|
it must be a numeric string.
|
128 |
|
|
.PP
|
129 |
|
|
The fifth part of a conversion specifier is a length modifier,
|
130 |
|
|
which must be \fBh\fR or \fBl\fR.
|
131 |
|
|
If it is \fBh\fR it specifies that the numeric value should be
|
132 |
|
|
truncated to a 16-bit value before converting.
|
133 |
|
|
This option is rarely useful.
|
134 |
|
|
The \fBl\fR modifier is ignored.
|
135 |
|
|
.PP
|
136 |
|
|
The last thing in a conversion specifier is an alphabetic character
|
137 |
|
|
that determines what kind of conversion to perform.
|
138 |
|
|
The following conversion characters are currently supported:
|
139 |
|
|
.TP 10
|
140 |
|
|
\fBd\fR
|
141 |
|
|
Convert integer to signed decimal string.
|
142 |
|
|
.TP 10
|
143 |
|
|
\fBu\fR
|
144 |
|
|
Convert integer to unsigned decimal string.
|
145 |
|
|
.TP 10
|
146 |
|
|
\fBi\fR
|
147 |
|
|
Convert integer to signed decimal string; the integer may either be
|
148 |
|
|
in decimal, in octal (with a leading \fB0\fR) or in hexadecimal
|
149 |
|
|
(with a leading \fB0x\fR).
|
150 |
|
|
.TP 10
|
151 |
|
|
\fBo\fR
|
152 |
|
|
Convert integer to unsigned octal string.
|
153 |
|
|
.TP 10
|
154 |
|
|
\fBx\fR or \fBX\fR
|
155 |
|
|
Convert integer to unsigned hexadecimal string, using digits
|
156 |
|
|
``0123456789abcdef'' for \fBx\fR and ``0123456789ABCDEF'' for \fBX\fR).
|
157 |
|
|
.TP 10
|
158 |
|
|
\fBc\fR
|
159 |
|
|
Convert integer to the 8-bit character it represents.
|
160 |
|
|
.TP 10
|
161 |
|
|
\fBs\fR
|
162 |
|
|
No conversion; just insert string.
|
163 |
|
|
.TP 10
|
164 |
|
|
\fBf\fR
|
165 |
|
|
Convert floating-point number to signed decimal string of
|
166 |
|
|
the form \fIxx.yyy\fR, where the number of \fIy\fR's is determined by
|
167 |
|
|
the precision (default: 6).
|
168 |
|
|
If the precision is 0 then no decimal point is output.
|
169 |
|
|
.TP 10
|
170 |
|
|
\fBe\fR or \fBe\fR
|
171 |
|
|
Convert floating-point number to scientific notation in the
|
172 |
|
|
form \fIx.yyy\fBe\(+-\fIzz\fR, where the number of \fIy\fR's is determined
|
173 |
|
|
by the precision (default: 6).
|
174 |
|
|
If the precision is 0 then no decimal point is output.
|
175 |
|
|
If the \fBE\fR form is used then \fBE\fR is
|
176 |
|
|
printed instead of \fBe\fR.
|
177 |
|
|
.TP 10
|
178 |
|
|
\fBg\fR or \fBG\fR
|
179 |
|
|
If the exponent is less than \-4 or greater than or equal to the
|
180 |
|
|
precision, then convert floating-point number as for \fB%e\fR or
|
181 |
|
|
\fB%E\fR.
|
182 |
|
|
Otherwise convert as for \fB%f\fR.
|
183 |
|
|
Trailing zeroes and a trailing decimal point are omitted.
|
184 |
|
|
.TP 10
|
185 |
|
|
\fB%\fR
|
186 |
|
|
No conversion: just insert \fB%\fR.
|
187 |
|
|
.LP
|
188 |
|
|
For the numerical conversions the argument being converted must
|
189 |
|
|
be an integer or floating-point string; format converts the argument
|
190 |
|
|
to binary and then converts it back to a string according to
|
191 |
|
|
the conversion specifier.
|
192 |
|
|
|
193 |
|
|
.SH "DIFFERENCES FROM ANSI SPRINTF"
|
194 |
|
|
.PP
|
195 |
|
|
The behavior of the format command is the same as the
|
196 |
|
|
ANSI C \fBsprintf\fR procedure except for the following
|
197 |
|
|
differences:
|
198 |
|
|
.IP [1]
|
199 |
|
|
\fB%p\fR and \fB%n\fR specifiers are not currently supported.
|
200 |
|
|
.IP [2]
|
201 |
|
|
For \fB%c\fR conversions the argument must be a decimal string,
|
202 |
|
|
which will then be converted to the corresponding character value.
|
203 |
|
|
.IP [3]
|
204 |
|
|
The \fBl\fR modifier is ignored; integer values are always converted
|
205 |
|
|
as if there were no modifier present and real values are always
|
206 |
|
|
converted as if the \fBl\fR modifier were present (i.e. type
|
207 |
|
|
\fBdouble\fR is used for the internal representation).
|
208 |
|
|
If the \fBh\fR modifier is specified then integer values are truncated
|
209 |
|
|
to \fBshort\fR before conversion.
|
210 |
|
|
|
211 |
|
|
.SH KEYWORDS
|
212 |
|
|
conversion specifier, format, sprintf, string, substitution
|