File: printf_conversion.xml

package info (click to toggle)
scilab 5.2.2-9
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 334,832 kB
  • ctags: 52,586
  • sloc: xml: 526,945; ansic: 223,590; fortran: 163,080; java: 56,934; cpp: 33,840; tcl: 27,936; sh: 20,397; makefile: 9,908; ml: 9,451; perl: 1,323; cs: 614; lisp: 30
file content (319 lines) | stat: -rw-r--r-- 13,843 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<?xml version="1.0" encoding="UTF-8"?>
<!--
 * Scilab ( http://www.scilab.org/ ) - This file is part of Scilab
 * Copyright (C) XXXX-2008 - INRIA
 * 
 * This file must be used under the terms of the CeCILL.
 * This source file is licensed as described in the file COPYING, which
 * you should have received as part of this distribution.  The terms
 * are also available at    
 * http://www.cecill.info/licences/Licence_CeCILL_V2-en.txt
 *
 -->
<refentry version="5.0-subset Scilab" xml:id="printf_conversion" xml:lang="en"
          xmlns="http://docbook.org/ns/docbook"
          xmlns:xlink="http://www.w3.org/1999/xlink"
          xmlns:svg="http://www.w3.org/2000/svg"
          xmlns:ns4="http://www.w3.org/1999/xhtml"
          xmlns:mml="http://www.w3.org/1998/Math/MathML"
          xmlns:db="http://docbook.org/ns/docbook">
  <info>
    <pubdate>$LastChangedDate$</pubdate>
  </info>
  
  <refnamediv>
    <refname>printf_conversion</refname>

    <refpurpose>printf, sprintf, fprintf conversion
    specifications</refpurpose>
  </refnamediv>

  <refsection>
    <title>Description</title>

    <para>Each conversion specification in the <literal>printf</literal> ,
    <literal>sprintf</literal> , <literal>fprintf</literal>
    <literal>format</literal> parameter has the following syntax:</para>

    <itemizedlist>
      <listitem>
        <para>A % (percent) sign.</para>
      </listitem>

      <listitem>
        <para>Zero or more <literal>options</literal>, which modify the
        meaning of the conversion specification. The following list contains
        the <literal>option</literal> characters and their meanings:</para>

        <itemizedlist>
          <listitem>
            <para>- : Left align, within the field, the result of the
            conversion.</para>
          </listitem>

          <listitem>
            <para>+ : Begin the result of a signed conversion with a sign (+
            or -).</para>
          </listitem>

          <listitem>
            <para>'space' : Prefix a space character to the result if the
            first character of a signed conversion is not a sign. If both the
            (space) and + options appear, the (space) option is ignored</para>
          </listitem>

          <listitem>
            <para># : Convert the value to an alternate form. For
            <literal>c</literal>, <literal>d</literal>, <literal>i</literal>,
            <literal>s</literal>, and <literal>u</literal> conversions, the
            <literal>#</literal> option has no effect. For
            <literal>o</literal> conversion, <literal>#</literal> increases
            the precision to force the first digit of the result to be a 0
            (zero). For <literal>x</literal> and <literal>X</literal>
            conversions, a nonzero result has 0x or 0X prefixed to it. For
            <literal>e, E, f, g,</literal> and <literal>G</literal>
            conversions, the result always contains a decimal point, even if
            no digits follow it. For <literal>g</literal> and
            <literal>G</literal> conversions, trailing zeros are not removed
            from the result.</para>
          </listitem>

          <listitem>
            <para>0 : Pad to the field width, using leading zeros (following
            any indication of sign or base) for <literal>d</literal>,
            <literal>i</literal>, <literal>o</literal>, <literal>u</literal>,
            <literal>x</literal>, <literal>X</literal>, <literal>e</literal>,
            <literal>E</literal>, <literal>f</literal>, <literal>g</literal>,
            and <literal>G</literal> conversions; no space padding is
            performed. If the <literal>0</literal> and <literal>\-</literal>
            (dash) flags both appear, the <literal>0</literal> flag is
            ignored. For <literal>d</literal>, <literal>i</literal>,
            <literal>o</literal> <literal>u</literal>, <literal>x</literal>,
            and <literal>X</literal> conversions, if a precision is specified,
            the <literal>0</literal> flag is also ignored.</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>

    <para>An optional decimal digit string that specifies the minimum field
    width. If the converted value has fewer characters than the field width,
    the field is padded on the left to the length specified by the field
    width. If the left-adjustment option is specified, the field is padded on
    the right.</para>

    <para>An optional precision. The precision is a <literal>.</literal> (dot)
    followed by a decimal digit string. If no precision is given, the
    parameter is treated as 0 (zero). The precision specifies:</para>

    <itemizedlist>
      <listitem>
        <para>The minimum number of digits to appear for <literal>d</literal>,
        <literal>u</literal>, <literal>o</literal>, <literal>x</literal>, or
        <literal>X</literal> conversions</para>
      </listitem>

      <listitem>
        <para>The number of digits to appear after the decimal point for
        <literal>e</literal>, <literal>E</literal>, and <literal>f</literal>
        conversions</para>
      </listitem>

      <listitem>
        <para>The maximum number of significant digits for
        <literal>g</literal> and <literal>G</literal> conversions</para>
      </listitem>

      <listitem>
        <para>The maximum number of characters to be printed from a string in
        an <literal>s</literal> conversion</para>
      </listitem>

      <listitem>
        <para>A character that indicates the type of conversion to be
        applied:</para>

        <itemizedlist>
          <listitem>
            <para>% : Performs no conversion. Displays %.</para>
          </listitem>

          <listitem>
            <para>d,i: Accepts an integer <literal>value</literal> and
            converts it to signed decimal notation. The precision specifies
            the minimum number of digits to appear. If the value being
            converted can be represented in fewer digits, it is expanded with
            leading zeros. The default precision is 1. The result of
            converting a zero value with a precision of zero is a null string.
            Specifying a field width with a zero as a leading character causes
            the field width value to be padded with leading zeros.</para>
          </listitem>

          <listitem>
            <para>u : Accepts an integer <literal>value</literal> and converts
            it to unsigned decimal notation. The precision specifies the
            minimum number of digits to appear. If the value being converted
            can be represented in fewer digits, it is expanded with leading
            zeros. The default precision is 1. The result of converting a zero
            value with a precision of zero is a null string. Specifying a
            field width with a zero as the leading character causes the field
            width value to be padded with leading zeros.</para>
          </listitem>

          <listitem>
            <para>o : Accepts an integer <literal>value</literal> and converts
            it to unsigned octal notation. The precision specifies the minimum
            number of digits to appear. If the value being converted can be
            represented in fewer digits, it is expanded with leading zeros.
            The default precision is 1. The result of converting a zero value
            with a precision of zero is a null string. Specifying a field
            width with a zero as the leading character causes the field width
            value to be padded with leading zeros. An octal value for field
            width is not implied.</para>
          </listitem>

          <listitem>
            <para>x, X : Accepts an integer <literal>value</literal> and
            converts it to unsigned hexadecimal notation. The letters
            ``abcdef'' are used for the <literal>x</literal> conversion; the
            letters ``ABCDEF'' are used for the <literal>X</literal>
            conversion. The precision specifies the minimum number of digits
            to appear. If the value being converted can be represented in
            fewer digits, it is expanded with leading zeros. The default
            precision is 1. The result of converting a zero value with a
            precision of zero is a null string. Specifying a field width with
            a zero as the leading character causes the field width value to be
            padded with leading zeros.</para>
          </listitem>

          <listitem>
            <para>f : Accepts a float or double <literal>value</literal> and
            converts it to decimal notation in the format
            %[\-]<literal>ddd.ddd</literal>. The number of digits after the
            decimal point is equal to the precision specification.</para>

            <itemizedlist>
              <listitem>
                <para>If no precision is specified, six digits are
                output.</para>
              </listitem>

              <listitem>
                <para>If the precision is zero, no decimal point appears and
                the system outputs a number rounded to the integer nearest to
                <literal>value</literal>.</para>
              </listitem>

              <listitem>
                <para>If a decimal point is output, at least one digit is
                output before it.</para>
              </listitem>
            </itemizedlist>
          </listitem>

          <listitem>
            <para>e, E : Accepts a real and converts it to the exponential
            form %[\-]<literal>d.ddde</literal>+/\-<literal>dd</literal>.
            There is one digit before the decimal point, and the number of
            digits after the decimal point is equal to the precision
            specification.</para>

            <itemizedlist>
              <listitem>
                <para>If no precision is specified, six digits are
                output.</para>
              </listitem>

              <listitem>
                <para>If the precision is zero, , no decimal point
                appears.</para>
              </listitem>

              <listitem>
                <para>The <literal>E</literal> conversion character produces a
                number with E instead of e before the exponent. The exponent
                always contains at least two digits. If the value is zero, the
                exponent is zero.</para>
              </listitem>
            </itemizedlist>
          </listitem>

          <listitem>
            <para>g, G : Accepts a real and converts it in the style of the
            <literal>e</literal>, <literal>E</literal>, or
            <literal>f</literal> conversion characters, with the precision
            specifying the number of significant digits. Trailing zeros are
            removed from the result. A decimal point appears only if it is
            followed by a digit. The style used depends on the value
            converted. Style <literal>e</literal> (<literal>E</literal>, if
            <literal>G</literal> is the flag used) results only if the
            exponent resulting from the conversion is less than -4, or if it
            is greater or equal to the precision.</para>
          </listitem>

          <listitem>
            <para>c : Accepts and displays an integer value converted to a
            character.</para>
          </listitem>

          <listitem>
            <para>s : Accepts a string <literal>value</literal> and displays
            characters from the string to the end or the number of characters
            indicated by the precision is reached. If no precision is
            specified, all characters up to the end are displayed.</para>
          </listitem>
        </itemizedlist>
      </listitem>
    </itemizedlist>

    <para>A field width or precision can be indicated by an
    <literal>*</literal> (asterisk) instead of a digit string. In this case,
    an integer <literal>value</literal> parameter supplies the field width or
    precision. The <literal>value</literal> parameter converted for output is
    not fetched until the conversion letter is reached, so the parameters
    specifying field width or precision must appear before the value to be
    converted (if any).</para>

    <para>If the result of a conversion is wider than the field width, the
    field is expanded to contain the converted result.</para>

    <para>The representation of the plus sign depends on whether the
    <literal>+</literal> or (space) formatting option is specified.</para>
  </refsection>

  <refsection>
    <title>Examples</title>

    <programlisting role="example"><![CDATA[ 
printf('a string: %s\n', 'Scilab');
printf('an integer: %d\n', 10);
printf('an integer: %4d\n', 10);
printf('a left justified integer: %-4d\n', 10);
printf('an integer converted to float: %#fd\n',10);
printf('an integer with a sign: %+4d\n', 10);
printf('an integer with a sign: %+4d\n', -10);
printf('an integer padded with zeros: %04d\n', 10);
printf('an unsigned integer: %u\n', 10);
printf('an unsigned integer: %4u\n', -10);
printf('an integer converted to hexadecimal: %x\n', 10);
printf('a float: %d\n', %pi);
printf('a float: %3.2d\n', %pi);
printf('a float (exponential form): %3.2e\n', %pi);
printf('a float (exponential form): %3.2g\n', %pi);
printf('a character: %c\n', 'a');
printf('a character: %c\n', 'aaa');
 ]]></programlisting>
  </refsection>

  <refsection>
    <title>See Also</title>

    <simplelist type="inline">
      <member><link linkend="printf">printf</link></member>

      <member><link linkend="fprintf">fprintf</link></member>

      <member><link linkend="sprintf">sprintf</link></member>
    </simplelist>
  </refsection>
</refentry>