写给愿意动手的 PHP 开发者
PHP学习网 PHP学习网
公告PHP学习网为公益性网站,致力于为广大PHP爱好者服务
数学与数字 PHP PHP 4 number_format

number_format()

格式化数字,加千分位、控制小数位。金额展示的标准做法。

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

参数

参数类型说明
$numfloat要格式化的数字
$decimalsint保留几位小数
$decimal_separatorstring小数点符号
$thousands_separatorstring千分位符号

返回值

string — 格式化后的字符串。

示例

PHP
echo number_format(1234567.891, 2);
echo ' ';
echo number_format(1234567.891, 2, '.', '');
输出
1,234,567.89 1234567.89

注意

  • 返回值是字符串,参与计算前要转回数字。
  • 第四参数传空字符串可以去掉千分位,做接口数据时常用。

相关函数