How to display Currency in Indian Numbering Format

2019-01-08 17:10发布

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

Refer Indian Numbering System

I have to do with it PHP.

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

Update:

How to use money_format() in indian currency format?

20条回答
姐就是有狂的资本
2楼-- · 2019-01-08 17:40

When money_format is not available :

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;

}
查看更多
混吃等死
3楼-- · 2019-01-08 17:41

Check this code its work 100% for indian Rs format with decimal setting u can use

123456.789 123.456 123.4 123 and 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="$des0";}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.

}
查看更多
甜甜的少女心
4楼-- · 2019-01-08 17:41
<?php
    function moneyFormatIndia($num) 
    {
        //$num=123456789.00;
        $result='';
        $sum=explode('.',$num);
        $after_dec=$sum[1];
        $before_dec=$sum[0];
        $result='.'.$after_dec;
        $num=$before_dec;
        $len=strlen($num);
        if($len<=3) 
        {
            $result=$num.$result;
        }
        else
        {
            if($len<=5)
            {
                $result='Rs '.substr($num, 0,$len-3).','.substr($num,$len-3).$result;
                return $result;
            }
            else
            {
                $ls=strlen($num);
                $result=substr($num, $ls-5,2).','.substr($num, $ls-3).$result;
                $num=substr($num, 0,$ls-5);
                while(strlen($num)!=0)
                {
                    $result=','.$result;
                    $ls=strlen($num);
                    if($ls<=2)
                    {
                        $result='Rs. '.$num.$result;
                        return $result;
                    }
                    else
                    {
                        $result=substr($num, $ls-2).$result;
                        $num=substr($num, 0,$ls-2);
                    }
                }
            }
        }
    }
?>
查看更多
劫难
5楼-- · 2019-01-08 17:45

You have so many options but money_format can do the trick for you.

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.

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.
}
查看更多
ゆ 、 Hurt°
6楼-- · 2019-01-08 17:46

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;
}
查看更多
贼婆χ
7楼-- · 2019-01-08 17:47

Simply use below function to format in INR.

function amount_inr_format($amount) {
    $fmt = new \NumberFormatter($locale = 'en_IN', NumberFormatter::DECIMAL);
    return $fmt->format($amount);
}
查看更多
登录 后发表回答