<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <HTML ><HEAD ><TITLE >number_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="nl2br" HREF="function.nl2br.html"><LINK REL="NEXT" TITLE="ord" HREF="function.ord.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.nl2br.html" ACCESSKEY="P" >上一页</A ></TD ><TD WIDTH="80%" ALIGN="center" VALIGN="bottom" ></TD ><TD WIDTH="10%" ALIGN="right" VALIGN="bottom" ><A HREF="function.ord.html" ACCESSKEY="N" >下一页</A ></TD ></TR ></TABLE ><HR ALIGN="LEFT" WIDTH="100%"></DIV ><H1 ><A NAME="function.number-format" ></A >number_format</H1 ><DIV CLASS="refnamediv" ><A NAME="AEN226311" ></A ><P > (PHP 3, PHP 4, PHP 5)</P >number_format -- Format a number with grouped thousands</DIV ><DIV CLASS="refsect1" ><A NAME="AEN226314" ></A ><H2 >Description</H2 >string <B CLASS="methodname" >number_format</B > ( float number [, int decimals [, string dec_point, string thousands_sep]] )<BR ></BR ><P > <B CLASS="function" >number_format()</B > returns a formatted version of <CODE CLASS="parameter" >number</CODE >. This function accepts either one, two or four parameters (not three): </P ><P >If only one parameter is given, <CODE CLASS="parameter" >number</CODE > will be formatted without decimals, but with a comma (",") between every group of thousands. </P ><P > If two parameters are given, <CODE CLASS="parameter" >number</CODE > will be formatted with <CODE CLASS="parameter" >decimals</CODE > decimals with a dot (".") in front, and a comma (",") between every group of thousands. </P ><P > If all four parameters are given, <CODE CLASS="parameter" >number</CODE > will be formatted with <CODE CLASS="parameter" >decimals</CODE > decimals, <CODE CLASS="parameter" >dec_point</CODE > instead of a dot (".") before the decimals and <CODE CLASS="parameter" >thousands_sep</CODE > instead of a comma (",") between every group of thousands. </P ><P > Only the first character of <CODE CLASS="parameter" >thousands_sep</CODE > is used. For example, if you use <TT CLASS="literal" >bar</TT > as <CODE CLASS="parameter" >thousands_sep</CODE > on the number <TT CLASS="literal" >1000</TT >, <B CLASS="function" >number_format()</B > will return <TT CLASS="literal" >1b000</TT >. </P ><P > <TABLE WIDTH="100%" BORDER="0" CELLPADDING="0" CELLSPACING="0" CLASS="EXAMPLE" ><TR ><TD ><DIV CLASS="example" ><A NAME="AEN226352" ></A ><P ><B >例 1. <B CLASS="function" >number_format()</B > Example</B ></P ><P > For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. This is achieved with this line : </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">// english notation (default)<br /></font><font color="#0000BB">$english_format_number </font><font color="#007700">= </font><font color="#0000BB">number_format</font><font color="#007700">(</font><font color="#0000BB">$number</font><font color="#007700">);<br /></font><font color="#FF8000">// 1,235<br /><br />// French notation<br /></font><font color="#0000BB">$nombre_format_francais </font><font color="#007700">= </font><font color="#0000BB">number_format</font><font color="#007700">(</font><font color="#0000BB">$number</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#DD0000">','</font><font color="#007700">, </font><font color="#DD0000">' '</font><font color="#007700">);<br /></font><font color="#FF8000">// 1 234,56<br /><br /></font><font color="#0000BB">$number </font><font color="#007700">= </font><font color="#0000BB">1234.5678</font><font color="#007700">;<br /><br /></font><font color="#FF8000">// english notation without thousands seperator<br /></font><font color="#0000BB">$english_format_number </font><font color="#007700">= </font><font color="#0000BB">number_format</font><font color="#007700">(</font><font color="#0000BB">$number</font><font color="#007700">, </font><font color="#0000BB">2</font><font color="#007700">, </font><font color="#DD0000">'.'</font><font color="#007700">, </font><font color="#DD0000">''</font><font color="#007700">);<br /></font><font color="#FF8000">// 1234.57<br /><br /></font><font color="#0000BB">?></font> </font> </code></TD ></TR ></TABLE ></DIV ></TD ></TR ></TABLE > </P ><P > See also: <A HREF="function.money-format.html" ><B CLASS="function" >money_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.nl2br.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.ord.html" ACCESSKEY="N" >下一页</A ></TD ></TR ><TR ><TD WIDTH="33%" ALIGN="left" VALIGN="top" >nl2br</TD ><TD WIDTH="34%" ALIGN="center" VALIGN="top" ><A HREF="ref.strings.html" ACCESSKEY="U" >上一级</A ></TD ><TD WIDTH="33%" ALIGN="right" VALIGN="top" >ord</TD ></TR ></TABLE ></DIV ></BODY ></HTML >