<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >money_format</TITLE ><META NAME="GENERATOR" CONTENT="Modular DocBook HTML Stylesheet Version 1.79"><LINK REL="HOME" TITLE="PHP 手册" HREF="index.html"><LINK REL="UP" TITLE="String 字符串处理函数" HREF="ref.strings.html"><LINK REL="PREVIOUS" TITLE="metaphone" HREF="function.metaphone.html"><LINK REL="NEXT" TITLE="nl_langinfo" HREF="function.nl-langinfo.html"><META HTTP-EQUIV="Content-type" CONTENT="text/html; charset=UTF-8"></HEAD ><BODY CLASS="refentry" BGCOLOR="#FFFFFF" TEXT="#000000" LINK="#0000FF" VLINK="#840084" ALINK="#0000FF" ><DIV CLASS="NAVHEADER" ><TABLE SUMMARY="Header navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TH COLSPAN="3" ALIGN="center" >PHP 手册</TH ></TR ><TR ><TD WIDTH="10%" ALIGN="left" VALIGN="bottom" ><A HREF="function.metaphone.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.nl-langinfo.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.money-format" ></A >money_format</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN225938" ></A ><P > (PHP 4 >= 4.3.0, PHP 5)</P >money_format -- Formats a number as a currency string</DIV ><DIV CLASS="refsect1" ><A NAME="AEN225941" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >money_format</B > ( string format, float number )<BR ></BR ><P > <B CLASS="function" >money_format()</B > returns a formatted version of <CODE CLASS="parameter" >number</CODE >. This function wraps the C library function <B CLASS="function" >strfmon()</B >, with the difference that this implementation converts only one number at a time. </P ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The function <B CLASS="function" >money_format()</B > is only defined if the system has strfmon capabilities. For example, Windows does not, so <B CLASS="function" >money_format()</B > is undefined in Windows. </P ></BLOCKQUOTE ></DIV ><P > The format specification consists of the following sequence: <P ></P ><UL ><LI ><P >a <TT CLASS="literal" >%</TT > character</P ></LI ><LI ><P >optional flags</P ></LI ><LI ><P >optional field width</P ></LI ><LI ><P >optional left precision</P ></LI ><LI ><P >optional right precision</P ></LI ><LI ><P >a required conversion character</P ></LI ></UL > </P ><DIV CLASS="formalpara" ><P ><B >Flags. </B > One or more of the optional flags below can be used: <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="literal" >=</TT ><TT CLASS="replaceable" ><I >f</I ></TT ></DT ><DD ><P > The character <TT CLASS="literal" >=</TT > followed by a (single byte) character <TT CLASS="replaceable" ><I >f</I ></TT > to be used as the numeric fill character. The default fill character is space. </P ></DD ><DT ><TT CLASS="literal" >^</TT ></DT ><DD ><P > Disable the use of grouping characters (as defined by the current locale). </P ></DD ><DT ><TT CLASS="literal" >+</TT > or <TT CLASS="literal" >(</TT ></DT ><DD ><P > Specify the formatting style for positive and negative numbers. If <TT CLASS="literal" >+</TT > is used, the locale's equivalent for <TT CLASS="literal" >+</TT > and <TT CLASS="literal" >-</TT > will be used. If <TT CLASS="literal" >(</TT > is used, negative amounts are enclosed in parenthesis. If no specification is given, the default is <TT CLASS="literal" >+</TT >. </P ></DD ><DT ><TT CLASS="literal" >!</TT ></DT ><DD ><P > Suppress the currency symbol from the output string. </P ></DD ><DT ><TT CLASS="literal" >-</TT ></DT ><DD ><P > 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 ></DL ></DIV > </P ></DIV ><DIV CLASS="formalpara" ><P ><B >Field width. </B > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="replaceable" ><I >w</I ></TT ></DT ><DD ><P > A decimal digit string specifying a minimum field width. Field will be right-justified unless the flag <TT CLASS="literal" >-</TT > is used. Default value is 0 (zero). </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="formalpara" ><P ><B >Left precision. </B > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="literal" >#</TT ><TT CLASS="replaceable" ><I >n</I ></TT ></DT ><DD ><P > The maximum number of digits (<TT CLASS="replaceable" ><I >n</I ></TT >) 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 <TT CLASS="replaceable" ><I >n</I ></TT >. If the number of actual digits is bigger than <TT CLASS="replaceable" ><I >n</I ></TT >, then this specification is ignored. </P ><P > If grouping has not been suppressed using the <TT CLASS="literal" >^</TT > 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 > 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 ></DL ></DIV > </P ></DIV ><DIV CLASS="formalpara" ><P ><B > Right precision . </B > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="literal" >.</TT ><TT CLASS="replaceable" ><I >p</I ></TT ></DT ><DD ><P > A period followed by the number of digits (<TT CLASS="replaceable" ><I >p</I ></TT >) after the decimal character. If the value of <TT CLASS="replaceable" ><I >p</I ></TT > 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 ></DL ></DIV > </P ></DIV ><DIV CLASS="formalpara" ><P ><B > Conversion characters . </B > <P ></P ><DIV CLASS="variablelist" ><DL ><DT ><TT CLASS="literal" >i</TT ></DT ><DD ><P > 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 ><TT CLASS="literal" >n</TT ></DT ><DD ><P > The number is formatted according to the locale's national currency format (e.g. for the de_DE locale: DM1.234,56). </P ></DD ><DT ><TT CLASS="literal" >%</TT ></DT ><DD ><P > Returns the <TT CLASS="literal" >%</TT > character. </P ></DD ></DL ></DIV > </P ></DIV ><DIV CLASS="note" ><BLOCKQUOTE CLASS="note" ><P ><B >注意: </B > The <TT CLASS="constant" ><B >LC_MONETARY</B ></TT > category of the locale settings, affects the behavior of this function. Use <A HREF="function.setlocale.html" ><B CLASS="function" >setlocale()</B ></A > to set to the appropriate default locale before using this function. </P ><P > Characters before and after the formatting string will be returned unchanged. </P ></BLOCKQUOTE ></DIV ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226077" ></A ><P ><B >例 1. <B CLASS="function" >money_format()</B > Example</B ></P ><P > We will use different locales and format specifications to illustrate the use of this function. </P ><TABLE BORDER="0" BGCOLOR="#E0E0E0" CELLPADDING="5" ><TR ><TD ><code><font color="#000000"> <font color="#0000BB"><?php<br /><br />$number </font><font color="#007700">= </font><font color="#0000BB">1234.56</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// let's print the international format for the en_US locale<br /></font><font color="#0000BB">setlocale</font><font color="#007700">(</font><font color="#0000BB">LC_MONETARY</font><font color="#007700">, </font><font color="#DD0000">'en_US'</font><font color="#007700">);<br />echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#DD0000">'%i'</font><font color="#007700">, </font><font color="#0000BB">$number</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">; <br /></font><font color="#FF8000">// USD 1,234.56<br /><br />// Italian national format with 2 decimals`<br /></font><font color="#0000BB">setlocale</font><font color="#007700">(</font><font color="#0000BB">LC_MONETARY</font><font color="#007700">, </font><font color="#DD0000">'it_IT'</font><font color="#007700">);<br />echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#DD0000">'%.2n'</font><font color="#007700">, </font><font color="#0000BB">$number</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// L. 1.234,56<br /><br />// Using a negative number<br /></font><font color="#0000BB">$number </font><font color="#007700">= -</font><font color="#0000BB">1234.5672</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// US national format, using () for negative numbers<br />// and 10 digits for left precision<br /></font><font color="#0000BB">setlocale</font><font color="#007700">(</font><font color="#0000BB">LC_MONETARY</font><font color="#007700">, </font><font color="#DD0000">'en_US'</font><font color="#007700">);<br />echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#DD0000">'%(#10n'</font><font color="#007700">, </font><font color="#0000BB">$number</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font 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 /></font><font color="#007700">echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#DD0000">'%=*(#10.2n'</font><font color="#007700">, </font><font color="#0000BB">$number</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font 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 /></font><font color="#0000BB">setlocale</font><font color="#007700">(</font><font color="#0000BB">LC_MONETARY</font><font color="#007700">, </font><font color="#DD0000">'de_DE'</font><font color="#007700">);<br />echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#DD0000">'%=*^-14#8.2i'</font><font color="#007700">, </font><font color="#0000BB">1234.56</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// DEM 1234,56****<br /><br />// Let's add some blurb before and after the conversion specification<br /></font><font color="#0000BB">setlocale</font><font color="#007700">(</font><font color="#0000BB">LC_MONETARY</font><font color="#007700">, </font><font color="#DD0000">'en_GB'</font><font color="#007700">);<br /></font><font color="#0000BB">$fmt </font><font color="#007700">= </font><font color="#DD0000">'The final value is %i (after a 10%% discount)'</font><font color="#007700">;<br />echo </font><font color="#0000BB">money_format</font><font color="#007700">(</font><font color="#0000BB">$fmt</font><font color="#007700">, </font><font color="#0000BB">1234.56</font><font color="#007700">) . </font><font color="#DD0000">"\n"</font><font color="#007700">;<br /></font><font color="#FF8000">// The final value is GBP 1,234.56 (after a 10% discount)<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also: <A HREF="function.setlocale.html" ><B CLASS="function" >setlocale()</B ></A >, <A HREF="function.number-format.html" ><B CLASS="function" >number_format()</B ></A >, <A HREF="function.sprintf.html" ><B CLASS="function" >sprintf()</B ></A >, <A HREF="function.printf.html" ><B CLASS="function" >printf()</B ></A > and <A HREF="function.sscanf.html" ><B CLASS="function" >sscanf()</B ></A >. </P ></DIV ><DIV CLASS="NAVFOOTER" ><HR ALIGN="LEFT" WIDTH="100%"><TABLE SUMMARY="Footer navigation table" WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" ><A HREF="function.metaphone.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="index.html" ACCESSKEY="H" >起始页</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" ><A HREF="function.nl-langinfo.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >metaphone</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >nl_langinfo</TD ></TR ></TABLE ></DIV ></BODY ></HTML >