number_format

(PHP 4, PHP 5, PHP 7, PHP 8)

number_format以千位分隔符方式格式化一个数字

说明

number_format(
    float $num,
    int $decimals = 0,
    ?string $decimal_separator = ".",
    ?string $thousands_separator = ","
): string

使用分组的千位和可选的小数位数格式化数字。

参数

num

要格式化的数字。

decimals

设置小数位数。如果为 0,则从返回值中忽略 decimal_separator

decimal_separator

指定小数点的分隔符。

thousands_separator

设置千位分隔符。

返回值

num 的格式化版本。

更新日志

版本 说明
8.0.0 在此版本之前,number_format() 接受一个、两个或四个参数(不会是三个)。
7.2.0 number_format() 现在再也不会返回 -0,之前 num-0.01 的情况下可以返回 -0

范例

示例 #1 number_format() 示例

例如,法语计数通常使用两位小数,逗号(“,”)作为小数分隔符,空格(“ ”)作为千位分隔符。以下示例展示了格式化数字的各种方法:

<?php

$number
= 1234.56;

// 英文计数(默认)
$english_format_number = number_format($number);
// 1,235

// 法语计数
$nombre_format_francais = number_format($number, 2, ',', ' ');
// 1 234,56

$number = 1234.5678;

// 没有千位分隔符的英文计数
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

参见

add a note

User Contributed Notes

There are no user contributed notes for this page.
To Top