我想知道如何在数字中添加逗号。让我的问题变得简单。
我想改变这个:
1210 views
至:
1,210 views
并且:
14301
至
14,301
等等更大的数字。是否可以使用PHP功能?
来自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美分
以下代码对我有用,可能对您有所帮助。
$number = 1234.56;
echo number_format($number, 2, '.', ',');
//1,234.56
$number = 1234.56;
//Vietnam notation(comma for decimal point, dot for thousand separator)
$number_format_vietnam = number_format($number, 2, ',', '.');
//1.234,56
通常情况下,如果一个数字足够大,可以在其中包含逗号,那么您可能希望在小数点后没有任何数字 - 但如果您显示的值可能很小,则您需要显示这些小数位。有条件地应用number_format,您可以使用它来添加逗号并剪掉任何不相关的后点小数。
if($measurement1 > 999) {
//Adds commas in thousands and drops anything after the decimal point
$measurement1 = number_format($measurement1);
}
如果您显示从现实世界输入派生的计算值,则效果很好。