用逗号格式化数字PHP


Formatting numbers with commas PHP

如何正确使用逗号格式化数字。示例

我想10000变成10000我知道我可以用下面的代码

number_format(10000)

问题是,如果是一个带小数的数字,它往往会去掉小数。

例如:10000.50它将显示数字为10001

我如何绕过这一点,正确显示带小数和不带小数的数字。

此函数有一个明确的文档:

string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )

http://php.net/manual/de/function.number-format.php

使用php格式化数字的正确方法是使用内置函数number_format

string number_format ( float $number , int $decimals = 0 , string $dec_point = "." , string $thousands_sep = "," )有四个参数可以传递给函数:

  1. $number:这是您想要格式化的数字
  2. $decimals:这是您想要格式化的小数位数
  3. $dec_point:它是您想要使用的字符(如果需要),而不是通常用于表示小数点的"."
  4. $thousands_sep:这是您想要用来分隔数千个位置的字符,例如","

有关该功能的文档可在此处找到

因此,在您的问题中,您将用number_format(10000,2) 替换number_format(10000)

希望这能有所帮助。

干杯!

请使用此number_format(10000.50);输出格式为10001

number_format(10000,3);number_format(10000,2);