(PHP 4, PHP 5, PHP 7, PHP 8)
number_format — 以千位分隔符方式格式化一個數(shù)字
$number, int $decimals = 0): string$number,$decimals = 0,$dec_point = ".",$thousands_sep = ","本函數(shù)可以接受1個、2個或者4個參數(shù)(注意:不能是3個):
如果只提供第一個參數(shù),number的小數(shù)部分會被去掉
并且每個千位分隔符都是英文小寫逗號","
如果提供兩個參數(shù),number將保留小數(shù)點后的位數(shù)到你設(shè)定的值,其余同樓上
如果提供了四個參數(shù),number
將保留decimals個長度的小數(shù)部分,
小數(shù)點被替換為dec_point,千位分隔符替換為thousands_sep
number你要格式化的數(shù)字
decimals要保留的小數(shù)位數(shù)
dec_point指定小數(shù)點顯示的字符
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.
|
示例 #1 number_format() Example
For instance, French notation usually use two decimals, comma (',') as decimal separator, and space (' ') as thousand separator. The following example demonstrates various way to format a number:
<?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
?>
| 版本 | 說明 |
|---|---|
| 7.2.0 |
number_format() 現(xiàn)在無法返回 -0,之前可能返回 -0,因為 number 可能會是 -0.01。
|