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:47

Above Function Not working with Decimal

$amount = 10000034000.001;
$amount = moneyFormatIndia( $amount );
echo $amount;




function moneyFormatIndia($num){
        $nums = explode(".",$num);
        if(count($nums)>2){
            return "0";
        }else{
        if(count($nums)==1){
            $nums[1]="00";
        }
        $num = $nums[0];
        $explrestunits = "" ;
        if(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; 
            $expunit = str_split($restunits, 2);
            for($i=0; $i<sizeof($expunit); $i++){

                if($i==0)
                {
                    $explrestunits .= (int)$expunit[$i].","; 
                }else{
                    $explrestunits .= $expunit[$i].",";
                }
            }
            $thecash = $explrestunits.$lastthree;
        } else {
            $thecash = $num;
        }
        return $thecash.".".$nums[1]; 
        }
    }

Answer : 10,00,00,34,000.001

查看更多
贼婆χ
3楼-- · 2019-01-08 17:47

If you dont want to use any inbuilt function in my case i was doing on iis server so was unable to use one the function in php so did this

$num = -21324322.23;


moneyFormatIndiaPHP($num);
function moneyFormatIndiaPHP($num){
    //converting it to string 
    $numToString = (string)$num;

    //take care of decimal values
    $change = explode('.', $numToString);

    //taking care of minus sign
    $checkifminus =  explode('-', $change[0]);


    //if minus then change the value as per
    $change[0] = (count($checkifminus) > 1)? $checkifminus[1] : $checkifminus[0];

    //store the minus sign for further
    $min_sgn = '';
    $min_sgn = (count($checkifminus) > 1)?'-':'';



    //catch the last three
    $lastThree = substr($change[0], strlen($change[0])-3);



    //catch the other three
    $ExlastThree = substr($change[0], 0 ,strlen($change[0])-3);


    //check whethr empty 
    if($ExlastThree != '')
        $lastThree = ',' . $lastThree;


    //replace through regex
    $res = preg_replace("/\B(?=(\d{2})+(?!\d))/",",",$ExlastThree);

    //main container num
    $lst = '';

    if(isset($change[1]) == ''){
        $lst =  $min_sgn.$res.$lastThree;
    }else{
        $lst =  $min_sgn.$res.$lastThree.".".$change[1];
    }

    //special case if equals to 2 then 
    if(strlen($change[0]) === 2){
        $lst = str_replace(",","",$lst);
    }

    return $lst;
}
查看更多
小情绪 Triste *
4楼-- · 2019-01-08 17:50

This for both integer and float values

    function indian_money_format($number)
    {

        if(strstr($number,"-"))
        {
            $number = str_replace("-","",$number);
            $negative = "-";
        }

        $split_number = @explode(".",$number);

        $rupee = $split_number[0];
        $paise = @$split_number[1];

        if(@strlen($rupee)>3)
        {
            $hundreds = substr($rupee,strlen($rupee)-3);
            $thousands_in_reverse = strrev(substr($rupee,0,strlen($rupee)-3));
            $thousands = '';
            for($i=0; $i<(strlen($thousands_in_reverse)); $i=$i+2)
            {
                $thousands .= $thousands_in_reverse[$i].$thousands_in_reverse[$i+1].",";
            }
            $thousands = strrev(trim($thousands,","));
            $formatted_rupee = $thousands.",".$hundreds;

        }
        else
        {
            $formatted_rupee = $rupee;
        }

        if((int)$paise>0)
        {
            $formatted_paise = ".".substr($paise,0,2);
        }else{
            $formatted_paise = '.00';
        }

        return $negative.$formatted_rupee.$formatted_paise;

    }
查看更多
放我归山
5楼-- · 2019-01-08 17:50

heres is simple thing u can do ,

 float amount = 100000;

 NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));

 String moneyString = formatter.format(amount);

 System.out.println(moneyString);

The output will be , Rs.100,000.00 .

查看更多
闹够了就滚
6楼-- · 2019-01-08 17:51

The example you've linked is making use of the ICU libraries which are available with PHP in the intl Extension­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
查看更多
唯我独甜
7楼-- · 2019-01-08 17:52
$r=explode('.',12345601.20);

$n = $r[0];
$len = strlen($n); //lenght of the no
$num = substr($n,$len-3,3); //get the last 3 digits
$n = $n/1000; //omit the last 3 digits already stored in $num
while($n > 0) //loop the process - further get digits 2 by 2
{
    $len = strlen($n);
    $num = substr($n,$len-2,2).",".$num;
    $n = round($n/100);
}
echo "Rs.".$num.'.'.$r[1];
查看更多
登录 后发表回答