如何在前三个字符之后添加句点,然后在'之后每3个字符添加
Example
$number = 1100000
Output = 1'100.000
Example2
$number = 560000
Output = 560.000
Example3
$number = 1000256000
Output = 1'000'256.000
我尝试了number_format但它会添加相同的符号(。,')...谢谢!!
$number = 1100000;
$english_format_number = number_format($number, 0, '.', "'");
Output: 1'100'000
$number = 1100000;
$english_format_number = number_format($number, 3, '.', "'");
Output: 1'100'000.000
$number = 1100000;
echo chunk_split($number,1,".");
谢谢!
答案 0 :(得分:3)
您使用number_format
走在正确的轨道上,但您必须指定所有4个参数才能获得您想要的结果。您还需要将数字除以1000以避免额外的小数位。
echo number_format($number / 1000, 3, "." , "'");
请参阅:http://php.net/manual/en/function.number-format.php
PHPFiddle:http://phpfiddle.org/main/code/e2c-a6q
答案 1 :(得分:0)
我最近不得不做类似的事情,尝试这个功能:
function formatMoney($number, $fractional=false) {
if ($fractional) {
$number = sprintf('%.2f', $number);
}
while (true) {
$replaced = preg_replace('/(-?\d+)(\d\d\d)/', '$1,$2', $number);
if ($replaced != $number) {
$number = $replaced;
} else {
break;
}
}
return $number;
}
编辑供您使用