| 1 |
'\" |
| 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 |
.TH format n 8.1 Tcl "Tcl Built-In Commands" |
| 9 |
.so man.macros |
| 10 |
.BS |
| 11 |
'\" Note: do not modify the .SH NAME line immediately below! |
| 12 |
.SH NAME |
| 13 |
format \- Format a string in the style of sprintf |
| 14 |
.SH SYNOPSIS |
| 15 |
\fBformat \fIformatString \fR?\fIarg arg ...\fR? |
| 16 |
.BE |
| 17 |
|
| 18 |
.SH INTRODUCTION |
| 19 |
.PP |
| 20 |
This command generates a formatted string in a fashion similar to the |
| 21 |
ANSI C \fBsprintf\fR procedure. |
| 22 |
\fIFormatString\fR indicates how to format the result, using |
| 23 |
\fB%\fR conversion specifiers as in \fBsprintf\fR, and the additional |
| 24 |
arguments, if any, provide values to be substituted into the result. |
| 25 |
The return value from \fBformat\fR is the formatted string. |
| 26 |
.SH "DETAILS ON FORMATTING" |
| 27 |
.PP |
| 28 |
The command operates by scanning \fIformatString\fR from left to right. |
| 29 |
Each character from the format string is appended to the result |
| 30 |
string unless it is a percent sign. |
| 31 |
If the character is a \fB%\fR then it is not copied to the result string. |
| 32 |
Instead, the characters following the \fB%\fR character are treated as |
| 33 |
a conversion specifier. |
| 34 |
The conversion specifier controls the conversion of the next successive |
| 35 |
\fIarg\fR to a particular format and the result is appended to |
| 36 |
the result string in place of the conversion specifier. |
| 37 |
If there are multiple conversion specifiers in the format string, |
| 38 |
then each one controls the conversion of one additional \fIarg\fR. |
| 39 |
The \fBformat\fR command must be given enough \fIarg\fRs to meet the needs |
| 40 |
of all of the conversion specifiers in \fIformatString\fR. |
| 41 |
.PP |
| 42 |
Each conversion specifier may contain up to six different parts: |
| 43 |
an XPG3 position specifier, |
| 44 |
a set of flags, a minimum field width, a precision, a size modifier, |
| 45 |
and a conversion character. |
| 46 |
Any of these fields may be omitted except for the conversion character. |
| 47 |
The fields that are present must appear in the order given above. |
| 48 |
The paragraphs below discuss each of these fields in turn. |
| 49 |
.SS "OPTIONAL POSITIONAL SPECIFIER" |
| 50 |
.PP |
| 51 |
If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in |
| 52 |
.QW \fB%2$d\fR , |
| 53 |
then the value to convert is not taken from the next sequential argument. |
| 54 |
Instead, it is taken from the argument indicated by the number, |
| 55 |
where 1 corresponds to the first \fIarg\fR. |
| 56 |
If the conversion specifier requires multiple arguments because |
| 57 |
of \fB*\fR characters in the specifier then |
| 58 |
successive arguments are used, starting with the argument |
| 59 |
given by the number. |
| 60 |
This follows the XPG3 conventions for positional specifiers. |
| 61 |
If there are any positional specifiers in \fIformatString\fR |
| 62 |
then all of the specifiers must be positional. |
| 63 |
.SS "OPTIONAL FLAGS" |
| 64 |
.PP |
| 65 |
The second portion of a conversion specifier may contain any of the |
| 66 |
following flag characters, in any order: |
| 67 |
.TP 10 |
| 68 |
\fB\-\fR |
| 69 |
Specifies that the converted argument should be left-justified |
| 70 |
in its field (numbers are normally right-justified with leading |
| 71 |
spaces if needed). |
| 72 |
.TP 10 |
| 73 |
\fB+\fR |
| 74 |
Specifies that a number should always be printed with a sign, |
| 75 |
even if positive. |
| 76 |
.TP 10 |
| 77 |
\fIspace\fR |
| 78 |
Specifies that a space should be added to the beginning of the |
| 79 |
number if the first character is not a sign. |
| 80 |
.TP 10 |
| 81 |
\fB0\fR |
| 82 |
Specifies that the number should be padded on the left with |
| 83 |
zeroes instead of spaces. |
| 84 |
.TP 10 |
| 85 |
\fB#\fR |
| 86 |
Requests an alternate output form. For \fBo\fR |
| 87 |
conversions it guarantees that the first digit is always \fB0\fR. |
| 88 |
For \fBx\fR or \fBX\fR conversions, \fB0x\fR or \fB0X\fR (respectively) |
| 89 |
will be added to the beginning of the result unless it is zero. |
| 90 |
For \fBb\fR conversions, \fB0b\fR |
| 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 |
.SS "OPTIONAL FIELD WIDTH" |
| 98 |
.PP |
| 99 |
The third portion of a conversion specifier is a decimal number giving a |
| 100 |
minimum field width for this conversion. |
| 101 |
It is typically used to make columns line up in tabular printouts. |
| 102 |
If the converted argument contains fewer characters than the |
| 103 |
minimum field width then it will be padded so that it is as wide |
| 104 |
as the minimum field width. |
| 105 |
Padding normally occurs by adding extra spaces on the left of the |
| 106 |
converted argument, but the \fB0\fR and \fB\-\fR flags |
| 107 |
may be used to specify padding with zeroes on the left or with |
| 108 |
spaces on the right, respectively. |
| 109 |
If the minimum field width is specified as \fB*\fR rather than |
| 110 |
a number, then the next argument to the \fBformat\fR command |
| 111 |
determines the minimum field width; it must be an integer value. |
| 112 |
.SS "OPTIONAL PRECISION/BOUND" |
| 113 |
.PP |
| 114 |
The fourth portion of a conversion specifier is a precision, |
| 115 |
which consists of a period followed by a number. |
| 116 |
The number is used in different ways for different conversions. |
| 117 |
For \fBe\fR, \fBE\fR, and \fBf\fR conversions it specifies the number |
| 118 |
of digits to appear to the right of the decimal point. |
| 119 |
For \fBg\fR and \fBG\fR conversions it specifies the total number |
| 120 |
of digits to appear, including those on both sides of the decimal |
| 121 |
point (however, trailing zeroes after the decimal point will still |
| 122 |
be omitted unless the \fB#\fR flag has been specified). |
| 123 |
For integer conversions, it specifies a minimum number of digits |
| 124 |
to print (leading zeroes will be added if necessary). |
| 125 |
For \fBs\fR conversions it specifies the maximum number of characters to be |
| 126 |
printed; if the string is longer than this then the trailing characters will be dropped. |
| 127 |
If the precision is specified with \fB*\fR rather than a number |
| 128 |
then the next argument to the \fBformat\fR command determines the precision; |
| 129 |
it must be a numeric string. |
| 130 |
.SS "OPTIONAL SIZE MODIFIER" |
| 131 |
.PP |
| 132 |
The fifth part of a conversion specifier is a size modifier, |
| 133 |
which must be \fBll\fR, \fBh\fR, or \fBl\fR. |
| 134 |
If it is \fBll\fR it specifies that an integer value is taken |
| 135 |
without truncation for conversion to a formatted substring. |
| 136 |
If it is \fBh\fR it specifies that an integer value is |
| 137 |
truncated to a 16-bit range before converting. This option is rarely useful. |
| 138 |
If it is \fBl\fR it specifies that the integer value is |
| 139 |
truncated to the same range as that produced by the \fBwide()\fR |
| 140 |
function of the \fBexpr\fR command (at least a 64-bit range). |
| 141 |
If neither \fBh\fR nor \fBl\fR are present, the integer value is |
| 142 |
truncated to the same range as that produced by the \fBint()\fR |
| 143 |
function of the \fBexpr\fR command (at least a 32-bit range, but |
| 144 |
determined by the value of the \fBwordSize\fR element of the |
| 145 |
\fBtcl_platform\fR array). |
| 146 |
.SS "MANDATORY CONVERSION TYPE" |
| 147 |
.PP |
| 148 |
The last thing in a conversion specifier is an alphabetic character |
| 149 |
that determines what kind of conversion to perform. |
| 150 |
The following conversion characters are currently supported: |
| 151 |
.TP 10 |
| 152 |
\fBd\fR |
| 153 |
Convert integer to signed decimal string. |
| 154 |
.TP 10 |
| 155 |
\fBu\fR |
| 156 |
Convert integer to unsigned decimal string. |
| 157 |
The conversion makes no sense without reference to a truncation range, |
| 158 |
so the size modifier \fBll\fR is not permitted in combination |
| 159 |
with conversion character \fBu\fR. |
| 160 |
.TP 10 |
| 161 |
\fBi\fR |
| 162 |
Convert integer to signed decimal string (equivalent to \fBd\fR). |
| 163 |
.TP 10 |
| 164 |
\fBo\fR |
| 165 |
Convert integer to unsigned octal string. |
| 166 |
.TP 10 |
| 167 |
\fBx\fR or \fBX\fR |
| 168 |
Convert integer to unsigned hexadecimal string, using digits |
| 169 |
.QW 0123456789abcdef |
| 170 |
for \fBx\fR and |
| 171 |
.QW 0123456789ABCDEF |
| 172 |
for \fBX\fR). |
| 173 |
.TP 10 |
| 174 |
\fBb\fR |
| 175 |
Convert integer to unsigned binary string, using digits 0 and 1. |
| 176 |
.TP 10 |
| 177 |
\fBc\fR |
| 178 |
Convert integer to the Unicode character it represents. |
| 179 |
.TP 10 |
| 180 |
\fBs\fR |
| 181 |
No conversion; just insert string. |
| 182 |
.TP 10 |
| 183 |
\fBf\fR |
| 184 |
Convert number to signed decimal string of |
| 185 |
the form \fIxx.yyy\fR, where the number of \fIy\fR's is determined by |
| 186 |
the precision (default: 6). |
| 187 |
If the precision is 0 then no decimal point is output. |
| 188 |
.TP 10 |
| 189 |
\fBe\fR or \fBE\fR |
| 190 |
Convert number to scientific notation in the |
| 191 |
form \fIx.yyy\fBe\(+-\fIzz\fR, where the number of \fIy\fR's is determined |
| 192 |
by the precision (default: 6). |
| 193 |
If the precision is 0 then no decimal point is output. |
| 194 |
If the \fBE\fR form is used then \fBE\fR is |
| 195 |
printed instead of \fBe\fR. |
| 196 |
.TP 10 |
| 197 |
\fBg\fR or \fBG\fR |
| 198 |
If the exponent is less than \-4 or greater than or equal to the |
| 199 |
precision, then convert number as for \fB%e\fR or |
| 200 |
\fB%E\fR. |
| 201 |
Otherwise convert as for \fB%f\fR. |
| 202 |
Trailing zeroes and a trailing decimal point are omitted. |
| 203 |
.TP 10 |
| 204 |
\fB%\fR |
| 205 |
No conversion: just insert \fB%\fR. |
| 206 |
.SH "DIFFERENCES FROM ANSI SPRINTF" |
| 207 |
.PP |
| 208 |
The behavior of the format command is the same as the |
| 209 |
ANSI C \fBsprintf\fR procedure except for the following |
| 210 |
differences: |
| 211 |
.IP [1] |
| 212 |
Tcl guarantees that it will be working with UNICODE characters. |
| 213 |
.IP [2] |
| 214 |
\fB%p\fR and \fB%n\fR specifiers are not supported. |
| 215 |
.IP [3] |
| 216 |
For \fB%c\fR conversions the argument must be an integer value, |
| 217 |
which will then be converted to the corresponding character value. |
| 218 |
.IP [4] |
| 219 |
The size modifiers are ignored when formatting floating-point values. |
| 220 |
The \fBll\fR modifier has no \fBsprintf\fR counterpart. |
| 221 |
The \fBb\fR specifier has no \fBsprintf\fR counterpart. |
| 222 |
.SH EXAMPLES |
| 223 |
.PP |
| 224 |
Convert the numeric value of a UNICODE character to the character |
| 225 |
itself: |
| 226 |
.PP |
| 227 |
.CS |
| 228 |
set value 120 |
| 229 |
set char [\fBformat\fR %c $value] |
| 230 |
.CE |
| 231 |
.PP |
| 232 |
Convert the output of \fBtime\fR into seconds to an accuracy of |
| 233 |
hundredths of a second: |
| 234 |
.PP |
| 235 |
.CS |
| 236 |
set us [lindex [time $someTclCode] 0] |
| 237 |
puts [\fBformat\fR "%.2f seconds to execute" [expr {$us / 1e6}]] |
| 238 |
.CE |
| 239 |
.PP |
| 240 |
Create a packed X11 literal color specification: |
| 241 |
.PP |
| 242 |
.CS |
| 243 |
# Each color-component should be in range (0..255) |
| 244 |
set color [\fBformat\fR "#%02x%02x%02x" $r $g $b] |
| 245 |
.CE |
| 246 |
.PP |
| 247 |
Use XPG3 format codes to allow reordering of fields (a technique that |
| 248 |
is often used in localized message catalogs; see \fBmsgcat\fR) without |
| 249 |
reordering the data values passed to \fBformat\fR: |
| 250 |
.PP |
| 251 |
.CS |
| 252 |
set fmt1 "Today, %d shares in %s were bought at $%.2f each" |
| 253 |
puts [\fBformat\fR $fmt1 123 "Global BigCorp" 19.37] |
| 254 |
|
| 255 |
set fmt2 "Bought %2\e$s equity ($%3$.2f x %1\e$d) today" |
| 256 |
puts [\fBformat\fR $fmt2 123 "Global BigCorp" 19.37] |
| 257 |
.CE |
| 258 |
.PP |
| 259 |
Print a small table of powers of three: |
| 260 |
.PP |
| 261 |
.CS |
| 262 |
# Set up the column widths |
| 263 |
set w1 5 |
| 264 |
set w2 10 |
| 265 |
|
| 266 |
# Make a nice header (with separator) for the table first |
| 267 |
set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+ |
| 268 |
puts $sep |
| 269 |
puts [\fBformat\fR "| %-*s | %-*s |" $w1 "Index" $w2 "Power"] |
| 270 |
puts $sep |
| 271 |
|
| 272 |
# Print the contents of the table |
| 273 |
set p 1 |
| 274 |
for {set i 0} {$i<=20} {incr i} { |
| 275 |
puts [\fBformat\fR "| %*d | %*ld |" $w1 $i $w2 $p] |
| 276 |
set p [expr {wide($p) * 3}] |
| 277 |
} |
| 278 |
|
| 279 |
# Finish off by printing the separator again |
| 280 |
puts $sep |
| 281 |
.CE |
| 282 |
.SH "SEE ALSO" |
| 283 |
scan(n), sprintf(3), string(n) |
| 284 |
.SH KEYWORDS |
| 285 |
conversion specifier, format, sprintf, string, substitution |
| 286 |
'\" Local Variables: |
| 287 |
'\" mode: nroff |
| 288 |
'\" End: |