home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / tcl2-73c.zip / tcl7.3 / doc / format.n < prev    next >
Text File  |  1993-08-05  |  10KB  |  234 lines

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