How to display Currency in Indian Numbering Format in PHP

后端 未结 22 1807
一整个雨季
一整个雨季 2020-12-01 06:14

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,00         


        
相关标签:
22条回答
  • 2020-12-01 07:03

    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.
    }
    
    0 讨论(0)
  • 2020-12-01 07:03

    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;
    
    }
    
    0 讨论(0)
  • 2020-12-01 07:03

    I have used different format parameters to money_format() for my output.

    setlocale(LC_MONETARY, 'en_IN');
    if (ctype_digit($amount) ) {
         // is whole number
         // if not required any numbers after decimal use this format 
         $amount = money_format('%!.0n', $amount);
    }
    else {
         // is not whole number
         $amount = money_format('%!i', $amount);
    }
    //$amount=10043445.7887 outputs 1,00,43,445.79
    //$amount=10043445 outputs 1,00,43,445
    
    0 讨论(0)
  • 2020-12-01 07:04

    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);
    }
    
    0 讨论(0)
  • 2020-12-01 07:07

    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;
    
        }
    
    0 讨论(0)
  • 2020-12-01 07:09

    It's my very own function to do the task

    function bd_money($num) {
        $pre = NULL; $sep = array(); $app = '00';
        $s=substr($num,0,1);
        if ($s=='-') {$pre= '-';$num = substr($num,1);}
        $num=explode('.',$num);
        if (count($num)>1) $app=$num[1];
        if (strlen($num[0])<4) return $pre . $num[0] . '.' . $app;
        $th=substr($num[0],-3);
        $hu=substr($num[0],0,-3);
        while(strlen($hu)>0){$sep[]=substr($hu,-2); $hu=substr($hu,0,-2);}
        return $pre.implode(',',array_reverse($sep)).','.$th.'.'.$app;
    }
    

    It took 0.0110 Seconds per THOUSAND query while number_format took 0.001 only. Always try to use PHP native functions only when performance is target issue.

    0 讨论(0)
提交回复
热议问题