| 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 | '\" RCS: @(#) $Id: format.n,v 1.7.2.1 2004/10/27 12:52:40 dkf Exp $ |
| 9 | '\" |
| 10 | '\" The definitions below are for supplemental macros used in Tcl/Tk |
| 11 | '\" manual entries. |
| 12 | '\" |
| 13 | '\" .AP type name in/out ?indent? |
| 14 | '\" Start paragraph describing an argument to a library procedure. |
| 15 | '\" type is type of argument (int, etc.), in/out is either "in", "out", |
| 16 | '\" or "in/out" to describe whether procedure reads or modifies arg, |
| 17 | '\" and indent is equivalent to second arg of .IP (shouldn't ever be |
| 18 | '\" needed; use .AS below instead) |
| 19 | '\" |
| 20 | '\" .AS ?type? ?name? |
| 21 | '\" Give maximum sizes of arguments for setting tab stops. Type and |
| 22 | '\" name are examples of largest possible arguments that will be passed |
| 23 | '\" to .AP later. If args are omitted, default tab stops are used. |
| 24 | '\" |
| 25 | '\" .BS |
| 26 | '\" Start box enclosure. From here until next .BE, everything will be |
| 27 | '\" enclosed in one large box. |
| 28 | '\" |
| 29 | '\" .BE |
| 30 | '\" End of box enclosure. |
| 31 | '\" |
| 32 | '\" .CS |
| 33 | '\" Begin code excerpt. |
| 34 | '\" |
| 35 | '\" .CE |
| 36 | '\" End code excerpt. |
| 37 | '\" |
| 38 | '\" .VS ?version? ?br? |
| 39 | '\" Begin vertical sidebar, for use in marking newly-changed parts |
| 40 | '\" of man pages. The first argument is ignored and used for recording |
| 41 | '\" the version when the .VS was added, so that the sidebars can be |
| 42 | '\" found and removed when they reach a certain age. If another argument |
| 43 | '\" is present, then a line break is forced before starting the sidebar. |
| 44 | '\" |
| 45 | '\" .VE |
| 46 | '\" End of vertical sidebar. |
| 47 | '\" |
| 48 | '\" .DS |
| 49 | '\" Begin an indented unfilled display. |
| 50 | '\" |
| 51 | '\" .DE |
| 52 | '\" End of indented unfilled display. |
| 53 | '\" |
| 54 | '\" .SO |
| 55 | '\" Start of list of standard options for a Tk widget. The |
| 56 | '\" options follow on successive lines, in four columns separated |
| 57 | '\" by tabs. |
| 58 | '\" |
| 59 | '\" .SE |
| 60 | '\" End of list of standard options for a Tk widget. |
| 61 | '\" |
| 62 | '\" .OP cmdName dbName dbClass |
| 63 | '\" Start of description of a specific option. cmdName gives the |
| 64 | '\" option's name as specified in the class command, dbName gives |
| 65 | '\" the option's name in the option database, and dbClass gives |
| 66 | '\" the option's class in the option database. |
| 67 | '\" |
| 68 | '\" .UL arg1 arg2 |
| 69 | '\" Print arg1 underlined, then print arg2 normally. |
| 70 | '\" |
| 71 | '\" RCS: @(#) $Id: man.macros,v 1.4 2000/08/25 06:18:32 ericm Exp $ |
| 72 | '\" |
| 73 | '\" # Set up traps and other miscellaneous stuff for Tcl/Tk man pages. |
| 74 | .if t .wh -1.3i ^B |
| 75 | .nr ^l \n(.l |
| 76 | .ad b |
| 77 | '\" # Start an argument description |
| 78 | .de AP |
| 79 | .ie !"\\$4"" .TP \\$4 |
| 80 | .el \{\ |
| 81 | . ie !"\\$2"" .TP \\n()Cu |
| 82 | . el .TP 15 |
| 83 | .\} |
| 84 | .ta \\n()Au \\n()Bu |
| 85 | .ie !"\\$3"" \{\ |
| 86 | \&\\$1 \\fI\\$2\\fP (\\$3) |
| 87 | .\".b |
| 88 | .\} |
| 89 | .el \{\ |
| 90 | .br |
| 91 | .ie !"\\$2"" \{\ |
| 92 | \&\\$1 \\fI\\$2\\fP |
| 93 | .\} |
| 94 | .el \{\ |
| 95 | \&\\fI\\$1\\fP |
| 96 | .\} |
| 97 | .\} |
| 98 | .. |
| 99 | '\" # define tabbing values for .AP |
| 100 | .de AS |
| 101 | .nr )A 10n |
| 102 | .if !"\\$1"" .nr )A \\w'\\$1'u+3n |
| 103 | .nr )B \\n()Au+15n |
| 104 | .\" |
| 105 | .if !"\\$2"" .nr )B \\w'\\$2'u+\\n()Au+3n |
| 106 | .nr )C \\n()Bu+\\w'(in/out)'u+2n |
| 107 | .. |
| 108 | .AS Tcl_Interp Tcl_CreateInterp in/out |
| 109 | '\" # BS - start boxed text |
| 110 | '\" # ^y = starting y location |
| 111 | '\" # ^b = 1 |
| 112 | .de BS |
| 113 | .br |
| 114 | .mk ^y |
| 115 | .nr ^b 1u |
| 116 | .if n .nf |
| 117 | .if n .ti 0 |
| 118 | .if n \l'\\n(.lu\(ul' |
| 119 | .if n .fi |
| 120 | .. |
| 121 | '\" # BE - end boxed text (draw box now) |
| 122 | .de BE |
| 123 | .nf |
| 124 | .ti 0 |
| 125 | .mk ^t |
| 126 | .ie n \l'\\n(^lu\(ul' |
| 127 | .el \{\ |
| 128 | .\" Draw four-sided box normally, but don't draw top of |
| 129 | .\" box if the box started on an earlier page. |
| 130 | .ie !\\n(^b-1 \{\ |
| 131 | \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 132 | .\} |
| 133 | .el \}\ |
| 134 | \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\l'|0u-1.5n\(ul' |
| 135 | .\} |
| 136 | .\} |
| 137 | .fi |
| 138 | .br |
| 139 | .nr ^b 0 |
| 140 | .. |
| 141 | '\" # VS - start vertical sidebar |
| 142 | '\" # ^Y = starting y location |
| 143 | '\" # ^v = 1 (for troff; for nroff this doesn't matter) |
| 144 | .de VS |
| 145 | .if !"\\$2"" .br |
| 146 | .mk ^Y |
| 147 | .ie n 'mc \s12\(br\s0 |
| 148 | .el .nr ^v 1u |
| 149 | .. |
| 150 | '\" # VE - end of vertical sidebar |
| 151 | .de VE |
| 152 | .ie n 'mc |
| 153 | .el \{\ |
| 154 | .ev 2 |
| 155 | .nf |
| 156 | .ti 0 |
| 157 | .mk ^t |
| 158 | \h'|\\n(^lu+3n'\L'|\\n(^Yu-1v\(bv'\v'\\n(^tu+1v-\\n(^Yu'\h'-|\\n(^lu+3n' |
| 159 | .sp -1 |
| 160 | .fi |
| 161 | .ev |
| 162 | .\} |
| 163 | .nr ^v 0 |
| 164 | .. |
| 165 | '\" # Special macro to handle page bottom: finish off current |
| 166 | '\" # box/sidebar if in box/sidebar mode, then invoked standard |
| 167 | '\" # page bottom macro. |
| 168 | .de ^B |
| 169 | .ev 2 |
| 170 | 'ti 0 |
| 171 | 'nf |
| 172 | .mk ^t |
| 173 | .if \\n(^b \{\ |
| 174 | .\" Draw three-sided box if this is the box's first page, |
| 175 | .\" draw two sides but no top otherwise. |
| 176 | .ie !\\n(^b-1 \h'-1.5n'\L'|\\n(^yu-1v'\l'\\n(^lu+3n\(ul'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 177 | .el \h'-1.5n'\L'|\\n(^yu-1v'\h'\\n(^lu+3n'\L'\\n(^tu+1v-\\n(^yu'\h'|0u'\c |
| 178 | .\} |
| 179 | .if \\n(^v \{\ |
| 180 | .nr ^x \\n(^tu+1v-\\n(^Yu |
| 181 | \kx\h'-\\nxu'\h'|\\n(^lu+3n'\ky\L'-\\n(^xu'\v'\\n(^xu'\h'|0u'\c |
| 182 | .\} |
| 183 | .bp |
| 184 | 'fi |
| 185 | .ev |
| 186 | .if \\n(^b \{\ |
| 187 | .mk ^y |
| 188 | .nr ^b 2 |
| 189 | .\} |
| 190 | .if \\n(^v \{\ |
| 191 | .mk ^Y |
| 192 | .\} |
| 193 | .. |
| 194 | '\" # DS - begin display |
| 195 | .de DS |
| 196 | .RS |
| 197 | .nf |
| 198 | .sp |
| 199 | .. |
| 200 | '\" # DE - end display |
| 201 | .de DE |
| 202 | .fi |
| 203 | .RE |
| 204 | .sp |
| 205 | .. |
| 206 | '\" # SO - start of list of standard options |
| 207 | .de SO |
| 208 | .SH "STANDARD OPTIONS" |
| 209 | .LP |
| 210 | .nf |
| 211 | .ta 5.5c 11c |
| 212 | .ft B |
| 213 | .. |
| 214 | '\" # SE - end of list of standard options |
| 215 | .de SE |
| 216 | .fi |
| 217 | .ft R |
| 218 | .LP |
| 219 | See the \\fBoptions\\fR manual entry for details on the standard options. |
| 220 | .. |
| 221 | '\" # OP - start of full description for a single option |
| 222 | .de OP |
| 223 | .LP |
| 224 | .nf |
| 225 | .ta 4c |
| 226 | Command-Line Name: \\fB\\$1\\fR |
| 227 | Database Name: \\fB\\$2\\fR |
| 228 | Database Class: \\fB\\$3\\fR |
| 229 | .fi |
| 230 | .IP |
| 231 | .. |
| 232 | '\" # CS - begin code excerpt |
| 233 | .de CS |
| 234 | .RS |
| 235 | .nf |
| 236 | .ta .25i .5i .75i 1i |
| 237 | .. |
| 238 | '\" # CE - end code excerpt |
| 239 | .de CE |
| 240 | .fi |
| 241 | .RE |
| 242 | .. |
| 243 | .de UL |
| 244 | \\$1\l'|0\(ul'\\$2 |
| 245 | .. |
| 246 | .TH format n 8.1 Tcl "Tcl Built-In Commands" |
| 247 | .BS |
| 248 | '\" Note: do not modify the .SH NAME line immediately below! |
| 249 | .SH NAME |
| 250 | format \- Format a string in the style of sprintf |
| 251 | .SH SYNOPSIS |
| 252 | \fBformat \fIformatString \fR?\fIarg arg ...\fR? |
| 253 | .BE |
| 254 | |
| 255 | .SH INTRODUCTION |
| 256 | .PP |
| 257 | This command generates a formatted string in the same way as the |
| 258 | ANSI C \fBsprintf\fR procedure (it uses \fBsprintf\fR in its |
| 259 | implementation). |
| 260 | \fIFormatString\fR indicates how to format the result, using |
| 261 | \fB%\fR conversion specifiers as in \fBsprintf\fR, and the additional |
| 262 | arguments, if any, provide values to be substituted into the result. |
| 263 | The return value from \fBformat\fR is the formatted string. |
| 264 | .SH "DETAILS ON FORMATTING" |
| 265 | .PP |
| 266 | The command operates by scanning \fIformatString\fR from left to right. |
| 267 | Each character from the format string is appended to the result |
| 268 | string unless it is a percent sign. |
| 269 | If the character is a \fB%\fR then it is not copied to the result string. |
| 270 | Instead, the characters following the \fB%\fR character are treated as |
| 271 | a conversion specifier. |
| 272 | The conversion specifier controls the conversion of the next successive |
| 273 | \fIarg\fR to a particular format and the result is appended to |
| 274 | the result string in place of the conversion specifier. |
| 275 | If there are multiple conversion specifiers in the format string, |
| 276 | then each one controls the conversion of one additional \fIarg\fR. |
| 277 | The \fBformat\fR command must be given enough \fIarg\fRs to meet the needs |
| 278 | of all of the conversion specifiers in \fIformatString\fR. |
| 279 | .PP |
| 280 | Each conversion specifier may contain up to six different parts: |
| 281 | an XPG3 position specifier, |
| 282 | a set of flags, a minimum field width, a precision, a length modifier, |
| 283 | and a conversion character. |
| 284 | Any of these fields may be omitted except for the conversion character. |
| 285 | The fields that are present must appear in the order given above. |
| 286 | The paragraphs below discuss each of these fields in turn. |
| 287 | .PP |
| 288 | If the \fB%\fR is followed by a decimal number and a \fB$\fR, as in |
| 289 | ``\fB%2$d\fR'', then the value to convert is not taken from the |
| 290 | next sequential argument. |
| 291 | Instead, it is taken from the argument indicated by the number, |
| 292 | where 1 corresponds to the first \fIarg\fR. |
| 293 | If the conversion specifier requires multiple arguments because |
| 294 | of \fB*\fR characters in the specifier then |
| 295 | successive arguments are used, starting with the argument |
| 296 | given by the number. |
| 297 | This follows the XPG3 conventions for positional specifiers. |
| 298 | If there are any positional specifiers in \fIformatString\fR |
| 299 | then all of the specifiers must be positional. |
| 300 | .PP |
| 301 | The second portion of a conversion specifier may contain any of the |
| 302 | following flag characters, in any order: |
| 303 | .TP 10 |
| 304 | \fB\-\fR |
| 305 | Specifies that the converted argument should be left-justified |
| 306 | in its field (numbers are normally right-justified with leading |
| 307 | spaces if needed). |
| 308 | .TP 10 |
| 309 | \fB+\fR |
| 310 | Specifies that a number should always be printed with a sign, |
| 311 | even if positive. |
| 312 | .TP 10 |
| 313 | \fIspace\fR |
| 314 | Specifies that a space should be added to the beginning of the |
| 315 | number if the first character isn't a sign. |
| 316 | .TP 10 |
| 317 | \fB0\fR |
| 318 | Specifies that the number should be padded on the left with |
| 319 | zeroes instead of spaces. |
| 320 | .TP 10 |
| 321 | \fB#\fR |
| 322 | Requests an alternate output form. For \fBo\fR and \fBO\fR |
| 323 | conversions it guarantees that the first digit is always \fB0\fR. |
| 324 | For \fBx\fR or \fBX\fR conversions, \fB0x\fR or \fB0X\fR (respectively) |
| 325 | will be added to the beginning of the result unless it is zero. |
| 326 | For all floating-point conversions (\fBe\fR, \fBE\fR, \fBf\fR, |
| 327 | \fBg\fR, and \fBG\fR) it guarantees that the result always |
| 328 | has a decimal point. |
| 329 | For \fBg\fR and \fBG\fR conversions it specifies that |
| 330 | trailing zeroes should not be removed. |
| 331 | .PP |
| 332 | The third portion of a conversion specifier is a number giving a |
| 333 | minimum field width for this conversion. |
| 334 | It is typically used to make columns line up in tabular printouts. |
| 335 | If the converted argument contains fewer characters than the |
| 336 | minimum field width then it will be padded so that it is as wide |
| 337 | as the minimum field width. |
| 338 | Padding normally occurs by adding extra spaces on the left of the |
| 339 | converted argument, but the \fB0\fR and \fB\-\fR flags |
| 340 | may be used to specify padding with zeroes on the left or with |
| 341 | spaces on the right, respectively. |
| 342 | If the minimum field width is specified as \fB*\fR rather than |
| 343 | a number, then the next argument to the \fBformat\fR command |
| 344 | determines the minimum field width; it must be a numeric string. |
| 345 | .PP |
| 346 | The fourth portion of a conversion specifier is a precision, |
| 347 | which consists of a period followed by a number. |
| 348 | The number is used in different ways for different conversions. |
| 349 | For \fBe\fR, \fBE\fR, and \fBf\fR conversions it specifies the number |
| 350 | of digits to appear to the right of the decimal point. |
| 351 | For \fBg\fR and \fBG\fR conversions it specifies the total number |
| 352 | of digits to appear, including those on both sides of the decimal |
| 353 | point (however, trailing zeroes after the decimal point will still |
| 354 | be omitted unless the \fB#\fR flag has been specified). |
| 355 | For integer conversions, it specifies a minimum number of digits |
| 356 | to print (leading zeroes will be added if necessary). |
| 357 | For \fBs\fR conversions it specifies the maximum number of characters to be |
| 358 | printed; if the string is longer than this then the trailing characters will be dropped. |
| 359 | If the precision is specified with \fB*\fR rather than a number |
| 360 | then the next argument to the \fBformat\fR command determines the precision; |
| 361 | it must be a numeric string. |
| 362 | .PP |
| 363 | The fifth part of a conversion specifier is a length modifier, |
| 364 | which must be \fBh\fR or \fBl\fR. |
| 365 | If it is \fBh\fR it specifies that the numeric value should be |
| 366 | truncated to a 16-bit value before converting. |
| 367 | This option is rarely useful. |
| 368 | .VS 8.4 |
| 369 | If it is \fBl\fR it specifies that the numeric value should be (at |
| 370 | least) a 64-bit value. If neither \fBh\fR nor \fBl\fR are present, |
| 371 | numeric values are interpreted as being values of the width of the |
| 372 | native machine word, as described by \fBtcl_platform(wordSize)\fR. |
| 373 | .VE |
| 374 | .PP |
| 375 | The last thing in a conversion specifier is an alphabetic character |
| 376 | that determines what kind of conversion to perform. |
| 377 | The following conversion characters are currently supported: |
| 378 | .TP 10 |
| 379 | \fBd\fR |
| 380 | Convert integer to signed decimal string. |
| 381 | .TP 10 |
| 382 | \fBu\fR |
| 383 | Convert integer to unsigned decimal string. |
| 384 | .TP 10 |
| 385 | \fBi\fR |
| 386 | Convert integer to signed decimal string; the integer may either be |
| 387 | in decimal, in octal (with a leading \fB0\fR) or in hexadecimal |
| 388 | (with a leading \fB0x\fR). |
| 389 | .TP 10 |
| 390 | \fBo\fR |
| 391 | Convert integer to unsigned octal string. |
| 392 | .TP 10 |
| 393 | \fBx\fR or \fBX\fR |
| 394 | Convert integer to unsigned hexadecimal string, using digits |
| 395 | ``0123456789abcdef'' for \fBx\fR and ``0123456789ABCDEF'' for \fBX\fR). |
| 396 | .VS |
| 397 | .TP 10 |
| 398 | \fBc\fR |
| 399 | Convert integer to the Unicode character it represents. |
| 400 | .VE |
| 401 | .TP 10 |
| 402 | \fBs\fR |
| 403 | No conversion; just insert string. |
| 404 | .TP 10 |
| 405 | \fBf\fR |
| 406 | Convert floating-point number to signed decimal string of |
| 407 | the form \fIxx.yyy\fR, where the number of \fIy\fR's is determined by |
| 408 | the precision (default: 6). |
| 409 | If the precision is 0 then no decimal point is output. |
| 410 | .TP 10 |
| 411 | \fBe\fR or \fBe\fR |
| 412 | Convert floating-point number to scientific notation in the |
| 413 | form \fIx.yyy\fBe\(+-\fIzz\fR, where the number of \fIy\fR's is determined |
| 414 | by the precision (default: 6). |
| 415 | If the precision is 0 then no decimal point is output. |
| 416 | If the \fBE\fR form is used then \fBE\fR is |
| 417 | printed instead of \fBe\fR. |
| 418 | .TP 10 |
| 419 | \fBg\fR or \fBG\fR |
| 420 | If the exponent is less than \-4 or greater than or equal to the |
| 421 | precision, then convert floating-point number as for \fB%e\fR or |
| 422 | \fB%E\fR. |
| 423 | Otherwise convert as for \fB%f\fR. |
| 424 | Trailing zeroes and a trailing decimal point are omitted. |
| 425 | .TP 10 |
| 426 | \fB%\fR |
| 427 | No conversion: just insert \fB%\fR. |
| 428 | .LP |
| 429 | For the numerical conversions the argument being converted must |
| 430 | be an integer or floating-point string; format converts the argument |
| 431 | to binary and then converts it back to a string according to |
| 432 | the conversion specifier. |
| 433 | .SH "DIFFERENCES FROM ANSI SPRINTF" |
| 434 | .PP |
| 435 | The behavior of the format command is the same as the |
| 436 | ANSI C \fBsprintf\fR procedure except for the following |
| 437 | differences: |
| 438 | .IP [1] |
| 439 | \fB%p\fR and \fB%n\fR specifiers are not currently supported. |
| 440 | .IP [2] |
| 441 | For \fB%c\fR conversions the argument must be a decimal string, |
| 442 | which will then be converted to the corresponding character value. |
| 443 | .IP [3] |
| 444 | The \fBl\fR modifier |
| 445 | .VS 8.4 |
| 446 | is ignored for real values and on 64-bit platforms, which are always |
| 447 | converted as if the \fBl\fR modifier were present (i.e. the types |
| 448 | \fBdouble\fR and \fBlong\fR are used for the internal representation |
| 449 | of real and integer values, respectively). |
| 450 | .VE 8.4 |
| 451 | If the \fBh\fR modifier is specified then integer values are truncated |
| 452 | to \fBshort\fR before conversion. Both \fBh\fR and \fBl\fR modifiers |
| 453 | are ignored on all other conversions. |
| 454 | .SH EXAMPLES |
| 455 | Convert the output of \fBtime\fR into seconds to an accuracy of |
| 456 | hundredths of a second: |
| 457 | .CS |
| 458 | set us [lindex [time $someTclCode] 0] |
| 459 | puts [\fBformat\fR "%.2f seconds to execute" [expr {$us / 1e6}]] |
| 460 | .CE |
| 461 | .PP |
| 462 | Create a packed X11 literal color specification: |
| 463 | .CS |
| 464 | # Each color-component should be in range (0..255) |
| 465 | set color [\fBformat\fR "#%02x%02x%02x" $r $g $b] |
| 466 | .CE |
| 467 | .PP |
| 468 | Use XPG3 format codes to allow reordering of fields (a technique that |
| 469 | is often used in localized message catalogs; see \fBmsgcat\fR) without |
| 470 | reordering the data values passed to \fBformat\fR: |
| 471 | .CS |
| 472 | set fmt1 "Today, %d shares in %s were bought at $%.2f each" |
| 473 | puts [\fBformat\fR $fmt1 123 "Global BigCorp" 19.37] |
| 474 | |
| 475 | set fmt2 "Bought %2\\$s equity ($%3$.2f x %1\\$d) today" |
| 476 | puts [\fBformat\fR $fmt2 123 "Global BigCorp" 19.37] |
| 477 | .CE |
| 478 | .PP |
| 479 | Print a small table of powers of three: |
| 480 | .CS |
| 481 | # Set up the column widths |
| 482 | set w1 5 |
| 483 | set w2 10 |
| 484 | |
| 485 | # Make a nice header (with separator) for the table first |
| 486 | set sep +-[string repeat - $w1]-+-[string repeat - $w2]-+ |
| 487 | puts $sep |
| 488 | puts [\fBformat\fR "| %-*s | %-*s |" $w1 "Index" $w2 "Power"] |
| 489 | puts $sep |
| 490 | |
| 491 | # Print the contents of the table |
| 492 | set p 1 |
| 493 | for {set i 0} {$i<=20} {incr i} { |
| 494 | puts [\fBformat\fR "| %*d | %*ld |" $w1 $i $w2 $p] |
| 495 | set p [expr {wide($p) * 3}] |
| 496 | } |
| 497 | |
| 498 | # Finish off by printing the separator again |
| 499 | puts $sep |
| 500 | .CE |
| 501 | |
| 502 | .SH "SEE ALSO" |
| 503 | scan(n), sprintf(3), string(n) |
| 504 | |
| 505 | .SH KEYWORDS |
| 506 | conversion specifier, format, sprintf, string, substitution |