我需要一种通过在合适的地方插入逗号来将数字转换为格式化方式的方法。可以使用正则表达式完成吗?
例子:
12345 => 12,345
1234567 =>1,234,567
最佳答案
不需要 regex ,您可以使用 number_format() 函数轻松完成。
echo number_format(12345); // prints 12,345
echo number_format(1234567); // prints 1,234,567
.
$arr = array(
1234567890,
123456789,
12345678,
1234567,
123456,
12345,
1234,
123,
);
foreach($arr as $num) {
echo number_format($num)."\n";
}
输出:
1,234,567,890
123,456,789
12,345,678
1,234,567
123,456
12,345
1,234
123
关于PHP:通过插入逗号来格式化数字字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2437188/