number_format
(PHP 4, PHP 5, PHP 7, PHP 8)
number_format — 以千位分隔符方式格式化一個數字
說明
number_format(float
$number, int $decimals = 0): stringnumber_format(
float
int
string
string
): string
float
$number,int
$decimals = 0,string
$dec_point = ".",string
$thousands_sep = ","): string
本函式可以接受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.
|
範例
示例 #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() 現在無法返回 -0,之前可能返回 -0,因為 number 可能會是 -0.01。
|