php 在php中添加逗号作为千位分隔符和浮点数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17780653/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:26:29  来源:igfitidea点击:

Add commas as thousands separator and floating point dot in php

phpcommanumber-formatting

提问by Andrew Liu

I have this

我有这个

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

the return of $subtotal is "1234567.00"how to modify the definition of $subtotal, make it like this "1,234,567.00"

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

回答by Techie

Below will output 1,234,567.00

下面会输出 1,234,567.00

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

Syntax

句法

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

But I advise you to use money_formatwhich will formats a number as a currency string

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

回答by Nathan Srivi

You have many options, but money_formatcan do the trick for you.

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

// Example:

$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;

// Output:

"1,00,000.00"

Please note that money_format()is only defined if the system has strfmoncapabilities. For example, Windows does not, so it is undefined in Windows.

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

Final edit: Here's a pure PHP Implementation that will work on any system:

最终编辑:这是一个可以在任何系统上运行的纯 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.
}

回答by Prasanth Bendra

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

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

string money_format ( string $format , float $number )

ex:

前任:

// 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

Note:The function money_format() is only defined if the system has strfmon capabilities. For example, Windows does not, so money_format() is undefined in Windows.

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

Note:The LC_MONETARY category of the locale settings, affects the behavior of this function. Use setlocale() to set to the appropriate default locale before using this function.

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

Use number_format: http://www.php.net/manual/en/function.number-format.php

使用number_format: http://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