Php 将数字转换为印度货币格式

Php 将数字转换为印度货币格式,php,money-format,Php,Money Format,我正在尝试将数字转换为印度货币格式,如 100000 = 1,00,000 我有用 $explrestunits = "" ; if(strlen($num)>3){ $lastthree = substr($num, strlen($num)-3, strlen($num)); $restunits = substr($num, 0, strlen($num)-3); // extracts the last three

我正在尝试将数字转换为印度货币格式,如

100000 = 1,00,000
我有用

$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.
$explorestunits=”“;
如果(strlen($num)>3){
$lastthree=substr($num,strlen($num)-3,strlen($num));
$restunits=substr($num,0,strlen($num)-3);//提取最后三位数字
$restunits=(strlen($restunits)%2==1)?“0”。$restunits:$restunits;//以2的格式分解剩余的数字,在开头添加一个零以保持2的分组。
$expunit=str_split($restunits,2);

对于($i=0;$i请尝试以下内容

<?php
$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
$amount=explode('.',$amount); //Comment this if you want amount value to be 1,00,000.00
echo $amount[0]; //Comment this if you want amount value to be 1,00,000.00
编辑:

1,00,000
但是当我用减号(-)加上10000(五位数)时 返回输出0,10000

应该是-10000你知道吗


如果设置
$amount='-10000',上面的代码将为您提供
-10000
money\u format()
取决于区域设置。在调用函数之前设置区域设置。您不需要编写自己的
money\u format()
function@User016,这不是任何重复,我想在我的函数中找到bug,而不是另一个解决方案。所以,请先阅读问题,然后发表评论。在发布问题之前,您必须检查是否存在类似的问题。Sameerya人非常了解这一点,但我在我的函数中发布了关于bug的问题,**不适用她的函数**@USER016您能告诉我如何获取小数点后的两位数字吗?不幸的是,这在windows平台上会失败…
money\u format()
将返回
未定义的
。编辑:我注意到OP已经在使用此功能,因此,这对他来说不是问题,但在回答本帖的未来访问者时值得一提