File: money-format.xml

package info (click to toggle)
phpdoc 20050512-1
  • links: PTS
  • area: non-free
  • in suites: sarge
  • size: 36,592 kB
  • ctags: 1,501
  • sloc: xml: 376,768; php: 6,708; cpp: 500; makefile: 293; perl: 161; sh: 151; awk: 28
file content (299 lines) | stat: -rw-r--r-- 10,070 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
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.8 $ -->
  <refentry id="function.money-format">
   <refnamediv>
    <refname>money_format</refname>
    <refpurpose>Formats a number as a currency string</refpurpose>
   </refnamediv>
   <refsect1>
    <title>Description</title>
     <methodsynopsis>
      <type>string</type><methodname>money_format</methodname>
      <methodparam><type>string</type><parameter>format</parameter></methodparam>
      <methodparam><type>float</type><parameter>number</parameter></methodparam>
     </methodsynopsis>
    <para>
     <function>money_format</function> returns a formatted version of
     <parameter>number</parameter>.  This function wraps the C library
     function <function>strfmon</function>, with the difference that
     this implementation converts only one number at a time.
    </para>
    <note>
     <para>
      The function <function>money_format</function> is only defined if 
      the system has strfmon capabilities.  For example, Windows does
      not, so <function>money_format</function> is undefined in Windows.
     </para>
    </note>
    <para>
     The format specification consists of the following sequence:
     <itemizedlist>
      <listitem><para>a <literal>%</literal> character</para></listitem>
      <listitem><para>optional flags</para></listitem>
      <listitem><para>optional field width</para></listitem>
      <listitem><para>optional left precision</para></listitem>
      <listitem><para>optional right precision</para></listitem>
      <listitem><para>a required conversion character</para></listitem>
     </itemizedlist>
    </para>
    <formalpara>
     <title>Flags</title>
     <para>
     One or more of the optional flags below can be used:
      <variablelist>
       <varlistentry>
        <term><literal>=</literal><replaceable>f</replaceable></term>
        <listitem>
         <para>
          The character <literal>=</literal> followed by a (single byte)
          character <replaceable>f</replaceable> to be used as the numeric fill
          character. The default fill character is space.
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>^</literal></term>
        <listitem>
         <para>
          Disable the use of grouping characters (as defined
          by the current locale).
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>+</literal> or <literal>(</literal></term>
        <listitem>
         <para>
          Specify the formatting style for positive and negative numbers.
          If <literal>+</literal> is used, the locale's equivalent for
          <literal>+</literal> and <literal>-</literal> will be used. If
          <literal>(</literal> is used, negative amounts are enclosed in
          parenthesis. If no specification is given, the default is
          <literal>+</literal>.
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>!</literal></term>
        <listitem>
         <para>
          Suppress the currency symbol from the output string.
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>-</literal></term>
        <listitem>
         <para>
          If present, it will make all fields left-justified (padded to the
          right), as opposed to the default which is for the fields to be
          right-justified (padded to the left).
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </para>
    </formalpara>
    <formalpara>
     <title>Field width</title>
     <para>
      <variablelist>
       <varlistentry>
        <term><replaceable>w</replaceable></term>
        <listitem>
         <para>
          A decimal digit string specifying a minimum field width. Field will
          be right-justified unless the flag <literal>-</literal> is used.
          Default value is 0 (zero).
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </para>
    </formalpara>
    <formalpara>
     <title>Left precision</title>
     <para>
      <variablelist>
       <varlistentry>
        <term><literal>#</literal><replaceable>n</replaceable></term>
        <listitem>
         <para>
          The maximum number of digits (<replaceable>n</replaceable>) expected
          to the left of the decimal character (e.g. the decimal point). It is
          used usually to keep formatted output aligned in the same columns,
          using the fill character if the number of digits is less than
          <replaceable>n</replaceable>. If the number of actual digits is
          bigger than <replaceable>n</replaceable>, then this specification is
          ignored.
         </para>
         <para>
          If grouping has not been suppressed using the <literal>^</literal>
          flag, grouping separators will be inserted before the fill
          characters (if any) are added. Grouping separators will not be
          applied to fill characters, even if the fill character is a digit.
         </para>
         <para>
          To ensure alignment, any characters appearing before or after the
          number in the formatted output such as currency or sign symbols are
          padded as necessary with space characters to make their positive and
          negative formats an equal length.
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </para>
    </formalpara>
    <formalpara>
     <title>
      Right precision
     </title>
     <para>
      <variablelist>
       <varlistentry>
        <term><literal>.</literal><replaceable>p</replaceable></term>
        <listitem>
         <para>
          A period followed by the number of digits
          (<replaceable>p</replaceable>) after the decimal character. If the
          value of <replaceable>p</replaceable> is 0 (zero), the decimal
          character and the digits to its right will be omitted. If no right
          precision is included, the default will dictated by the current
          local in use. The amount being formatted is rounded to the specified 
          number of digits prior to formatting.
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </para>
    </formalpara>
    <formalpara>
     <title>
      Conversion characters
     </title>
     <para>
      <variablelist>
       <varlistentry>
        <term><literal>i</literal></term>
        <listitem>
         <para>
          The number is formatted according to the locale's international
          currency format (e.g. for the USA locale: USD 1,234.56).
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>n</literal></term>
        <listitem>
         <para>
          The number is formatted according to the locale's national
          currency format (e.g. for the de_DE locale: DM1.234,56).
         </para>
        </listitem>
       </varlistentry>
       <varlistentry>
        <term><literal>%</literal></term>
        <listitem>
         <para>
          Returns the <literal>%</literal> character.
         </para>
        </listitem>
       </varlistentry>
      </variablelist>
     </para>
    </formalpara>
    <note>
     <para>
      The <constant>LC_MONETARY</constant> category of the locale settings,
      affects the behavior of this function. Use
      <function>setlocale</function> to set to the appropriate default locale
      before using this function.
     </para>
     <para>
      Characters before and after the formatting string will be returned
      unchanged.
     </para>
    </note>
    <para>
    <example>
     <title><function>money_format</function> Example</title>
     <para>
      We will use different locales and format specifications to
      illustrate the use of this function.
     </para>
      <programlisting role="php">
<![CDATA[
<?php

$number = 1234.56;

// let's print the international format for the en_US locale
setlocale(LC_MONETARY, 'en_US');
echo money_format('%i', $number) . "\n";  
// USD 1,234.56

// Italian national format with 2 decimals`
setlocale(LC_MONETARY, 'it_IT');
echo money_format('%.2n', $number) . "\n";
// L. 1.234,56

// Using a negative number
$number = -1234.5672;

// US national format, using () for negative numbers
// and 10 digits for left precision
setlocale(LC_MONETARY, 'en_US');
echo money_format('%(#10n', $number) . "\n";
// ($        1,234.57)

// Similar format as above, adding the use of 2 digits of right 
// precision and '*' as a fill character
echo money_format('%=*(#10.2n', $number) . "\n";
// ($********1,234.57)
    
// Let's justify to the left, with 14 positions of width, 8 digits of
// left precision, 2 of right precision, withouth grouping character
// and using the international format for the de_DE locale.
setlocale(LC_MONETARY, 'de_DE');
echo money_format('%=*^-14#8.2i', 1234.56) . "\n";
// DEM 1234,56****

// Let's add some blurb before and after the conversion specification
setlocale(LC_MONETARY, 'en_GB');
$fmt = 'The final value is %i (after a 10%% discount)';
echo money_format($fmt, 1234.56) . "\n";
// The final value is  GBP 1,234.56 (after a 10% discount)

?>
]]>
      </programlisting>
     </example>
    </para>
    <para>
     See also: <function>setlocale</function>,
     <function>number_format</function>,<function>sprintf</function>,
     <function>printf</function> and <function>sscanf</function>.
    </para>
   </refsect1>
  </refentry>

<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->