如何在PHP中以印度数字格式显示货币? [英] How to display Currency in Indian Numbering Format in PHP?
问题描述
我对格式化卢比(印度卢比-INR)存在疑问.
I have a question about formatting the Rupee currency (Indian Rupee - 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
引用印度编号系统
我必须使用PHP.
我已经看到了以下问题:以印度编号格式显示货币.但是无法用PHP来解决我的问题.
I have saw this question Displaying Currency in Indian Numbering Format. But couldn't able to get it for PHP my problem.
如何使用印度货币格式的 money_format()?
How to use money_format() in indian currency format?
推荐答案
您有很多选择,但 money_format
可以帮您解决问题.
You have so many options but money_format
can do the trick for you.
示例:
$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;
输出:
1,00,000.00
注意:
仅当系统具有strfmon功能时才定义函数 money_format().例如,Windows没有,因此在Windows中未定义 money_format().
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.
纯PHP实现-可在任何系统上使用:
Pure PHP Implementation - Works on any system:
$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.
}
这篇关于如何在PHP中以印度数字格式显示货币?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!