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

时间:2013-10-28 07:18:35

标签: php money-format

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

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.

但当我使用减号(-)10000(五位数)时,它会返回输出0,10,000

它应该是-10,000 你有什么想法..

1 个答案:

答案 0 :(得分:3)

尝试这样的事情

<?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,10,000

     

应该是-10,000你有什么想法..

如果设置-10,000

,上面的代码会为您提供 $amount = '-10000';