1

如何在前三个字符后添加一个句点,然后每 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,".");

谢谢!!

4

2 回答 2

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

于 2013-07-18T14:17:39.557 回答
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; 
} 

编辑供您使用

于 2013-07-18T14:20:59.873 回答