<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Formats a number as a currency string</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.metaphone.html">metaphone</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.nl-langinfo.html">nl_langinfo</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.money-format" class="refentry"> <div class="refnamediv"> <h1 class="refname">money_format</h1> <p class="verinfo">(PHP 4 >= 4.3.0, PHP 5)</p><p class="refpurpose"><span class="refname">money_format</span> — <span class="dc-title">Formats a number as a currency string</span></p> </div> <div class="refsect1 description" id="refsect1-function.money-format-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type">string</span> <span class="methodname"><strong>money_format</strong></span> ( <span class="methodparam"><span class="type">string</span> <code class="parameter">$format</code></span> , <span class="methodparam"><span class="type">float</span> <code class="parameter">$number</code></span> )</div> <p class="para rdfs-comment"> <span class="function"><strong>money_format()</strong></span> returns a formatted version of <em><code class="parameter">number</code></em>. This function wraps the C library function <span class="function"><strong>strfmon()</strong></span>, with the difference that this implementation converts only one number at a time. </p> </div> <div class="refsect1 parameters" id="refsect1-function.money-format-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">format</code></em></span> <dd> <p class="para"> The format specification consists of the following sequence: <ul class="itemizedlist"> <li class="listitem"><p class="para">a <em>%</em> character</p></li> <li class="listitem"><p class="para">optional flags</p></li> <li class="listitem"><p class="para">optional field width</p></li> <li class="listitem"><p class="para">optional left precision</p></li> <li class="listitem"><p class="para">optional right precision</p></li> <li class="listitem"><p class="para">a required conversion character</p></li> </ul> </p> <p class="formalpara"> <h5 class="title">Flags</h5> <p class="para"> One or more of the optional flags below can be used: <dl> <dt> <span class="term"><em>=</em><span class="replaceable">f</span></span> <dd> <p class="para"> The character <em>=</em> followed by a (single byte) character <span class="replaceable">f</span> to be used as the numeric fill character. The default fill character is space. </p> </dd> </dt> <dt> <span class="term"><em>^</em></span> <dd> <p class="para"> Disable the use of grouping characters (as defined by the current locale). </p> </dd> </dt> <dt> <span class="term"><em>+</em> or <em>(</em></span> <dd> <p class="para"> Specify the formatting style for positive and negative numbers. If <em>+</em> is used, the locale's equivalent for <em>+</em> and <em>-</em> will be used. If <em>(</em> is used, negative amounts are enclosed in parenthesis. If no specification is given, the default is <em>+</em>. </p> </dd> </dt> <dt> <span class="term"><em>!</em></span> <dd> <p class="para"> Suppress the currency symbol from the output string. </p> </dd> </dt> <dt> <span class="term"><em>-</em></span> <dd> <p class="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). </p> </dd> </dt> </dl> </p> </p> <p class="formalpara"> <h5 class="title">Field width</h5> <p class="para"> <dl> <dt> <span class="term"><span class="replaceable">w</span></span> <dd> <p class="para"> A decimal digit string specifying a minimum field width. Field will be right-justified unless the flag <em>-</em> is used. Default value is 0 (zero). </p> </dd> </dt> </dl> </p> </p> <p class="formalpara"> <h5 class="title">Left precision</h5> <p class="para"> <dl> <dt> <span class="term"><em>#</em><span class="replaceable">n</span></span> <dd> <p class="para"> The maximum number of digits (<span class="replaceable">n</span>) 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 <span class="replaceable">n</span>. If the number of actual digits is bigger than <span class="replaceable">n</span>, then this specification is ignored. </p> <p class="para"> If grouping has not been suppressed using the <em>^</em> 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. </p> <p class="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. </p> </dd> </dt> </dl> </p> </p> <p class="formalpara"> <h5 class="title"> Right precision </h5> <p class="para"> <dl> <dt> <span class="term"><em>.</em><span class="replaceable">p</span></span> <dd> <p class="para"> A period followed by the number of digits (<span class="replaceable">p</span>) after the decimal character. If the value of <span class="replaceable">p</span> 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. </p> </dd> </dt> </dl> </p> </p> <p class="formalpara"> <h5 class="title"> Conversion characters </h5> <p class="para"> <dl> <dt> <span class="term"><em>i</em></span> <dd> <p class="para"> The number is formatted according to the locale's international currency format (e.g. for the USA locale: USD 1,234.56). </p> </dd> </dt> <dt> <span class="term"><em>n</em></span> <dd> <p class="para"> The number is formatted according to the locale's national currency format (e.g. for the de_DE locale: EU1.234,56). </p> </dd> </dt> <dt> <span class="term"><em>%</em></span> <dd> <p class="para"> Returns the <em>%</em> character. </p> </dd> </dt> </dl> </p> </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">number</code></em></span> <dd> <p class="para"> The number to be formatted. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.money-format-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> Returns the formatted string. Characters before and after the formatting string will be returned unchanged. Non-numeric <em><code class="parameter">number</code></em> causes returning <strong><code>NULL</code></strong> and emitting <strong><code>E_WARNING</code></strong>. </p> </div> <div class="refsect1 notes" id="refsect1-function.money-format-notes"> <h3 class="title">Notes</h3> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The function <span class="function"><strong>money_format()</strong></span> is only defined if the system has strfmon capabilities. For example, Windows does not, so <span class="function"><strong>money_format()</strong></span> is undefined in Windows. </p> </p></blockquote> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> The <strong><code>LC_MONETARY</code></strong> category of the locale settings, affects the behavior of this function. Use <span class="function"><a href="function.setlocale.html" class="function">setlocale()</a></span> to set to the appropriate default locale before using this function. </p> </p></blockquote> </div> <div class="refsect1 examples" id="refsect1-function.money-format-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4849"> <p><strong>Example #1 <span class="function"><strong>money_format()</strong></span> Example</strong></p> <div class="example-contents"><p> We will use different locales and format specifications to illustrate the use of this function. </p></div> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /><br />$number </span><span style="color: #007700">= </span><span style="color: #0000BB">1234.56</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// let's print the international format for the en_US locale<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">, </span><span style="color: #DD0000">'en_US'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%i'</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// USD 1,234.56<br /><br />// Italian national format with 2 decimals`<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">, </span><span style="color: #DD0000">'it_IT'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%.2n'</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Eu 1.234,56<br /><br />// Using a negative number<br /></span><span style="color: #0000BB">$number </span><span style="color: #007700">= -</span><span style="color: #0000BB">1234.5672</span><span style="color: #007700">;<br /><br /></span><span style="color: #FF8000">// US national format, using () for negative numbers<br />// and 10 digits for left precision<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">, </span><span style="color: #DD0000">'en_US'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%(#10n'</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// ($ 1,234.57)<br /><br />// Similar format as above, adding the use of 2 digits of right<br />// precision and '*' as a fill character<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%=*(#10.2n'</span><span style="color: #007700">, </span><span style="color: #0000BB">$number</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// ($********1,234.57)<br /><br />// Let's justify to the left, with 14 positions of width, 8 digits of<br />// left precision, 2 of right precision, withouth grouping character<br />// and using the international format for the de_DE locale.<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">, </span><span style="color: #DD0000">'de_DE'</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #DD0000">'%=*^-14#8.2i'</span><span style="color: #007700">, </span><span style="color: #0000BB">1234.56</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// Eu 1234,56****<br /><br />// Let's add some blurb before and after the conversion specification<br /></span><span style="color: #0000BB">setlocale</span><span style="color: #007700">(</span><span style="color: #0000BB">LC_MONETARY</span><span style="color: #007700">, </span><span style="color: #DD0000">'en_GB'</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$fmt </span><span style="color: #007700">= </span><span style="color: #DD0000">'The final value is %i (after a 10%% discount)'</span><span style="color: #007700">;<br />echo </span><span style="color: #0000BB">money_format</span><span style="color: #007700">(</span><span style="color: #0000BB">$fmt</span><span style="color: #007700">, </span><span style="color: #0000BB">1234.56</span><span style="color: #007700">) . </span><span style="color: #DD0000">"\n"</span><span style="color: #007700">;<br /></span><span style="color: #FF8000">// The final value is GBP 1,234.56 (after a 10% discount)<br /><br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.money-format-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.setlocale.html" class="function" rel="rdfs-seeAlso">setlocale()</a> - Set locale information</span></li> <li class="member"> <span class="function"><a href="function.sscanf.html" class="function" rel="rdfs-seeAlso">sscanf()</a> - Parses input from a string according to a format</span></li> <li class="member"> <span class="function"><a href="function.sprintf.html" class="function" rel="rdfs-seeAlso">sprintf()</a> - Return a formatted string</span></li> <li class="member"> <span class="function"><a href="function.printf.html" class="function" rel="rdfs-seeAlso">printf()</a> - Output a formatted string</span></li> <li class="member"> <span class="function"><a href="function.number-format.html" class="function" rel="rdfs-seeAlso">number_format()</a> - Format a number with grouped thousands</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.metaphone.html">metaphone</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.nl-langinfo.html">nl_langinfo</a></div> <div class="up"><a href="ref.strings.html">String Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>