【问题标题】:Add commas as thousands separator and floating point dot in php在php中添加逗号作为千位分隔符和浮点点
【发布时间】:2013-07-20 18:42:18
【问题描述】:

我有这个

$example = "1234567"
$subtotal =  number_format($example, 2, '.', '');

$subtotal 的返回是"1234567.00" 如何修改$subtotal的定义,变成这样"1,234,567.00"

【问题讨论】:

  • number_format($example, 2, '.', ',');会为你做这件事..
  • 既然您是新来的,请接受解决您问题的答案(打勾)。向上投票(向上箭头)为您提供信息或帮助您的答案。向下投票(向下箭头)是假的答案。
  • 提示:如果你想使用长于 1 个字符的分隔符(例如“ ”),这在 php

标签: php comma number-formatting


【解决方案1】:

下面会输出1,234,567.00

$example = "1234567";
$subtotal =  number_format($example, 2, '.', ',');
echo $subtotal;

语法

string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )

但我建议你使用money_format,它将数字格式化为货币字符串

【讨论】:

    【解决方案2】:

    您有很多选择,但 money_format 可以为您解决问题。

    // Example:
    
    $amount = '100000';
    setlocale(LC_MONETARY, 'en_IN');
    $amount = money_format('%!i', $amount);
    echo $amount;
    
    // Output:
    
    "1,00,000.00"
    

    请注意,money_format() 仅在系统具有 strfmon 功能时才定义。比如windows没有,所以在windows中是未定义的。

    最终编辑:这是一个适用于任何系统的纯 PHP 实现:

    $amount = '10000034000';
    $amount = moneyFormatIndia( $amount );
    echo number_format($amount, 2, '.', '');
    
    function moneyFormatIndia($num){
        $explrestunits = "" ;
        if(strlen($num)>3){
            $lastthree = substr($num, strlen($num)-3, strlen($num));
            $restunits = substr($num, 0, strlen($num)-3); // extracts the last three digits
            $restunits = (strlen($restunits)%2 == 1)?"0".$restunits:$restunits; // explodes the remaining digits in 2's formats, adds a zero in the beginning to maintain the 2's grouping.
            $expunit = str_split($restunits, 2);
            for($i=0; $i<sizeof($expunit); $i++){
                // creates each of the 2's group and adds a comma to the end
                if($i==0){
                    $explrestunits .= (int)$expunit[$i].","; // if is first value , convert into integer
                }else{
                    $explrestunits .= $expunit[$i].",";
                }
            }
            $thecash = $explrestunits.$lastthree;
        } else {
            $thecash = $num;
        }
        return $thecash; // writes the final format where $currency is the currency symbol.
    }
    

    【讨论】:

    • 不知道我做错了什么,但它实际上并没有输出任何东西。我将改用@Dasun 的方法。还是谢谢
    • 检查一下我的纯 PHP 实现
    • 请问,第三行的number_format 是做什么的?就像为什么在这里,我阅读了 php.net 的定义,仍然不知道。实际上,如果没有它,您的函数可以正常工作,如果我包含第三行,则会引发错误。
    【解决方案3】:

    参考:http://php.net/manual/en/function.money-format.php

    string money_format ( string $format , float $number )
    

    例如:

    // let's print the international format for the en_US locale
    setlocale(LC_MONETARY, 'en_US');
    echo money_format('%i', $number) . "\n";
    // USD 1,234.56
    

    注意: 仅当系统具有 strfmon 功能时才定义函数 money_format()。例如,Windows 没有,因此 money_format() 在 Windows 中是未定义的。

    注意:区域设置的 LC_MONETARY 类别会影响此函数的行为。在使用此函数之前,请使用 setlocale() 设置为适当的默认语言环境。

    使用 number_formathttp://www.php.net/manual/en/function.number-format.php

    string number_format ( float $number , int $decimals = 0 , string $dec_point = '.' , string $thousands_sep = ',' )
    
    $number        = 123457;
    $format_number = number_format($number, 2, '.', ',');
    // 1,234.57
    

    【讨论】:

    • 我不知道我做错了什么,但它实际上并没有输出任何东西。我将改用@Dasun 的方法。还是谢谢
    • @LionLiu : money_format 不适用于 windows,我更新了答案,请检查。
    【解决方案4】:

    现在看来 money_format 已被弃用,所以即使您可以使用它,您也应该找到另一种格式化解决方案。

    https://www.php.net/manual/en/function.money-format.php

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-27
      • 2012-11-17
      • 1970-01-01
      相关资源
      最近更新 更多