如何在PHP中为数字添加逗号

php
2022-08-30 08:43:31

我想知道如何将逗号添加到数字中。为了使我的问题简单。

我想改变这一点:

1210 views

自:

1,210 views

和:

14301

14,301

以此类推,表示数字较大。可以使用php函数吗?


答案 1

从 php 手册 http://php.net/manual/en/function.number-format.php

我假设你想要英文格式。

<?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 with a decimal point and without thousands seperator
$english_format_number = number_format($number, 2, '.', '');
// 1234.57

?>

我的2美分


答案 2

下面的代码对我有用,也许这对你有帮助。

$number = 1234.56;

echo number_format($number, 2, '.', ',');

1,234.56


推荐