如何在 PHP 中以印度编号格式显示货币

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10042485/
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-24 21:13:06  来源:igfitidea点击:

How to display Currency in Indian Numbering Format in PHP

phpconvertercurrencynumber-formattingmoney-format

提问by Somnath Muluk

I have a question about formatting the Rupee currency (Indian Rupee - INR).

我有一个关于格式化卢比货币(印度卢比 - INR)的问题。

For example, numbers here are represented as:

例如,这里的数字表示为:

1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000

Refer Indian Numbering System

参考印度编号系统

I have to do with it PHP.

我必须用它来做 PHP。

I have saw this question Displaying Currency in Indian Numbering Format. But couldn't able to get it for PHP my problem.

我看到了这个问题Displaying Currency in Indian Numbering Format。但是无法为 PHP 获取它我的问题。

Update:

更新:

How to use money_format()in indian currency format?

如何以印度货币格式使用money_format()

回答by Baba

You have so 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

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 中是未定义的。

Pure PHP Implementation - Works on any system:

纯 PHP 实现 - 适用于任何系统:

$amount = '10000034000';
$amount = moneyFormatIndia( $amount );
echo $amount;

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 Vishal Chanana

echo 'Rs. '.IND_money_format(1234567890);

function IND_money_format($money){
    $len = strlen($money);
    $m = '';
    $money = strrev($money);
    for($i=0;$i<$len;$i++){
        if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len){
            $m .=',';
        }
        $m .=$money[$i];
    }
    return strrev($m);
}

NOTE:: it is not tested on float values and it suitable for only Integer

注意:它没有对浮点值进行测试,它只适用于整数

回答by hakre

The example you've linkedis making use of the ICU libraries which are available with PHP in the intl Extension­Docs:

你的例子链接是利用它可与PHP中的ICU库的国际扩展-Docs

$fmt = new NumberFormatter($locale = 'en_IN', NumberFormatter::CURRENCY);
echo $fmt->format(10000000000.1234)."\n"; # Rs?10,00,00,00,000.12

Or maybe better fitting in your case:

或者也许更适合您的情况:

$fmt = new NumberFormatter($locale = 'en_IN', NumberFormatter::DECIMAL);
echo $fmt->format(10000000000)."\n"; # 10,00,00,00,000

回答by Suresh Pandi

$num = 1234567890.123;

$num = preg_replace("/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/i", ",", $num);

echo $num;

// Input : 1234567890.123

// Output : 1,23,45,67,890.123


// Input : -1234567890.123

// Output : -1,23,45,67,890.123

回答by user3314233

Check this code, it works 100% for Indian Rupees format with decimal format. You can use numbers like :

检查此代码,它适用于十进制格式的印度卢比格式 100%。您可以使用以下数字:

123456.789 123.456 123.4 123 and 1,2,3,4,5,6,7,8,9,.222

123456.789 123.456 123.4 123 和 1,2,3,4,5,6,7,8,9,.222

function moneyFormatIndia($num){

$explrestunits = "" ;
$num = preg_replace('/,+/', '', $num);
$words = explode(".", $num);
$des = "00";
if(count($words)<=2){
    $num=$words[0];
    if(count($words)>=2){$des=$words[1];}
    if(strlen($des)<2){$des="$des";}else{$des=substr($des,0,2);}
}
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.$des"; // writes the final format where $currency is the currency symbol.

}

回答by Sonam Gurung

When money_format is not available :

当 money_format 不可用时:

function format($amount): string
{
    list ($number, $decimal) = explode('.', sprintf('%.2f', floatval($amount)));

    $sign = $number < 0 ? '-' : '';

    $number = abs($number);

    for ($i = 3; $i < strlen($number); $i += 3)
    {
        $number = substr_replace($number, ',', -$i, 0);
    }

    return $sign . $number . '.' . $decimal;

}

回答by Bishwanath Jha

Simply use below function to format in INR.

只需使用以下函数以 INR 进行格式化。

function amount_inr_format($amount) {
    $fmt = new \NumberFormatter($locale = 'en_IN', NumberFormatter::DECIMAL);
    return $fmt->format($amount);
}

回答by Kishore

$amount=-3000000000111.11;
$amount<0?(($sign='-').($amount*=-1)):$sign=''; //Extracting sign from given amount
$pos=strpos($amount, '.'); //Identifying the decimal point position
$amt=  substr($amount, $pos-3); // Extracting last 3 digits of integer part along with fractional part
$amount=  substr($amount,0, $pos-3); //removing the extracted part from amount
for(;strlen($amount);$amount=substr($amount,0,-2)) // Now loop through each 2 digits of remaining integer part
    $amt=substr ($amount,-2).','.$amt; //forming Indian Currency format by appending (,) for each 2 digits
echo $sign.$amt; //Appending sign

回答by Niet the Dark Absol

So if I'm reading that right, the Indian Numbering System separates the thousands, then every power of a hundred past that? Hmm...

所以如果我没看错的话,印度编号系统将千分之一分开,那么一百的每一个幂都超过那个?唔...

Perhaps something like this?

也许像这样?

function indian_number_format($num) {
    $num = "".$num;
    if( strlen($num) < 4) return $num;
    $tail = substr($num,-3);
    $head = substr($num,0,-3);
    $head = preg_replace("/\B(?=(?:\d{2})+(?!\d))/",",",$head);
    return $head.",".$tail;
}

回答by Elena

You should check the number_format function.Here is the link

您应该检查 number_format 函数。链接在这里

Separating thousands with commas will look like

用逗号分隔千位看起来像

$rupias = number_format($number, 2, ',', ',');