(PHP 4, PHP 5)
number_format — 以千位分隔符方式格式化一個數字
說明
string number_format ( float$number
[, int $decimals
= 0
] )
string number_format
( float $number
, int $decimals
= 0
, string $dec_point
= '.'
, string $thousands_sep
= ','
)
本函數可以接受1個、2個或者4個參數(注意:不能是3個):
如果只提供第一個參數,number
的小數部分會被去掉
并且每個千位分隔符都是英文小寫逗號","
如果提供兩個參數,number
將保留小數點后的位數到你設定的值,其余同樓上
如果提供了四個參數,number
將保留decimals
個長度的小數部分,
小數點被替換為dec_point
,千位分隔符替換為thousands_sep
參數
number
你要格式化的數字
decimals
要保留的小數位數
dec_point
指定小數點顯示的字符
thousands_sep
指定千位分隔符顯示的字符
返回值
格式化以后的 number
.
更新日志
版本
說明
5.4.0
This function now supports multiple bytes in
dec_point
and
thousands_sep
. Only the first byte of each
separator was used in older versions.
范例
Example #1 number_format() Example
For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. This is achieved with this line :
<?php
$number = 1234.56;
// english notation (default)
$english_format_number = number_format($number);
// 1,235
// French notation
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56
$number = 1234.5678;
// english notation without thousands separator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57
?>
參見
money_format() - Formats a number as a currency string sprintf() - Return a formatted string printf() - 輸出格式化字符串 sscanf() - 根據指定格式解析輸入的字符