【问题标题】:How to display Currency in Indian Numbering Format in PHP?如何在 PHP 中以印度编号格式显示货币?
【发布时间】:2012-04-20 00:18:01
【问题描述】:

我有一个关于格式化卢比货币(印度卢比 - INR)的问题。

例如,这里的数字表示为:

1
10
100
1,000
10,000
1,00,000
10,00,000
1,00,00,000
10,00,00,000

参考Indian Numbering System

我必须使用 PHP。

我看到了这个问题Displaying Currency in Indian Numbering Format。但是我的问题无法为 PHP 获取它。

更新:

如何在印度货币格式中使用money_format()

【问题讨论】:

  • 也许在这里php.net/manual/en/function.money-format.php你可以找到解决办法。
  • @lgomezma:如何在印度货币格式中使用 money_format()?
  • 在 php 7.4 中不推荐使用 money_format(),请使用 NumberFormatter

标签: php currency number-formatting converters money-format


【解决方案1】:

您有很多选择,但 money_format 可以为您解决问题。

例子:

$amount = '100000';
setlocale(LC_MONETARY, 'en_IN');
$amount = money_format('%!i', $amount);
echo $amount;

输出:

1,00,000.00

注意:

money_format() 函数仅在系统具有 strfmon 功能时才定义。例如,Windows 没有,因此 money_format() 在 Windows 中未定义。

纯 PHP 实现 - 适用于任何系统:

$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.
}

【讨论】:

  • 如果使用 windows 则使用this function for money_format()
  • @Somnath Muluk 在使用带有en_IN 的语言环境时没有给出准确的格式
  • 如果给定的数字是整数(round() 函数除外),是否有任何设置可以删除零后的零?
  • 功能不佳,无法处理小数和负数。
  • 谢谢。在花了 40 多分钟试图弄清楚为什么 money_format 不能在我的 Ubuntu 14.04 实例上运行之后,我在 Amazon 的 AWS EC2 上的 nginx 上运行,我现在解决了这个问题。通常的 money_format 在我当地的宅基地机器上运行良好,但无法让生产副本正常工作。这是一个 gist 使用 money_format 和 @Baba 的代码作为任何面临相同问题的人的后备。
【解决方案2】:
$num = 1234567890.123;

$num = preg_replace("/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/i", "$1,", $num);

echo $num;

// Input : 1234567890.123

// Output : 1,23,45,67,890.123


// Input : -1234567890.123

// Output : -1,23,45,67,890.123

【讨论】:

  • 我认为如果 LC_MONETARY 失败,这应该是后备方法。
  • 我正在使用 Windows 和 linux 主机,这种方法对两者都适用,这是将任何数字转换为 INR 格式的最佳和最短的方法。
【解决方案3】:
echo 'Rs. '.IND_money_format(1234567890);

function IND_money_format($money){
    $len = strlen($money);
    $m = '';
    $money = strrev($money);
    for($i=0;$i<$len;$i++){
        if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$len){
            $m .=',';
        }
        $m .=$money[$i];
    }
    return strrev($m);
}

注意:: 它未针对浮点值进行测试,仅适用于整数

【讨论】:

  • 因为 money_format() 在 Windows 上不起作用。这对我有用。
  • 这会为 5 位负值返回错误的输出。前任。对于 -10000,它返回 -,10,000。解决方案是=> 添加 if 条件,例如 -> if($money[$i]!="-"){ $m .=','; }
【解决方案4】:

linked 的示例正在使用intl Extension­Docs 中 PHP 提供的 ICU 库:

$fmt = new NumberFormatter($locale = 'en_IN', NumberFormatter::CURRENCY);
echo $fmt->format(10000000000.1234)."\n"; # Rs 10,00,00,00,000.12

或者也许更适合您的情况:

$fmt = new NumberFormatter($locale = 'en_IN', NumberFormatter::DECIMAL);
echo $fmt->format(10000000000)."\n"; # 10,00,00,00,000

【讨论】:

  • 适用于windows,给出的例子已经在windows上实际运行过,见PHP intl extension
  • codeigniter框架如何使用intl Extension­Docs:库?我无法使用此功能。
  • @SomnathMuluk:这只是 PHP,您可以按原样使用它。如果它看起来对你来说太复杂了,请将它包装成一个它自己的函数供初学者使用。
【解决方案5】:

只需使用下面的函数来格式化 INR。

function amount_inr_format($amount) {
    $fmt = new \NumberFormatter($locale = 'en_IN', NumberFormatter::DECIMAL);
    return $fmt->format($amount);
}

【讨论】:

    【解决方案6】:

    检查此代码,它适用于十进制格式的印度卢比格式 100%。 您可以使用以下数字:

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

    【讨论】:

      【解决方案7】:

      当 money_format 不可用时:

      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;
      
      }
      

      【讨论】:

        【解决方案8】:
        <?php
        $amount = '-100000.22222';    // output -1,00,000.22 
        //$amount = '0100000.22222';  // output 1,00,000.22 
        //$amount = '100000.22222';   // output 1,00,000.22 
        //$amount = '100000.';       // output 1,00,000.00 
        //$amount = '100000.2';     // output 1,00,000.20
        //$amount = '100000.0';    // output 1,00,000.00 
        //$amount = '100000';      // output 1,00,000.00 
        
        echo $aaa = moneyFormatIndia($amount);
        
        function moneyFormatIndia($amount)
            {
        
                $amount = round($amount,2);
        
                $amountArray =  explode('.', $amount);
                if(count($amountArray)==1)
                {
                    $int = $amountArray[0];
                    $des=00;
                }
                else {
                    $int = $amountArray[0];
                    $des=$amountArray[1];
                }
                if(strlen($des)==1)
                {
                    $des=$des."0";
                }
                if($int>=0)
                {
                    $int = numFormatIndia( $int );
                    $themoney = $int.".".$des;
                }
        
                else
                {
                    $int=abs($int);
                    $int = numFormatIndia( $int );
                    $themoney= "-".$int.".".$des;
                }   
                return $themoney;
            }
        
        function numFormatIndia($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.
            }
        ?>
        

        【讨论】:

          【解决方案9】:

          所以如果我没看错的话,印度编号系统将千位分开,然后百位的每一个幂?嗯……

          也许是这样的?

          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;
          }
          

          【讨论】:

          • 你需要对包含小数的数字做一些事情。
          【解决方案10】:
          $amount=-3000000000111.11;
          $amount<0?(($sign='-').($amount*=-1)):$sign=''; //Extracting sign from given amount
          $pos=strpos($amount, '.'); //Identifying the decimal point position
          $amt=  substr($amount, $pos-3); // Extracting last 3 digits of integer part along with fractional part
          $amount=  substr($amount,0, $pos-3); //removing the extracted part from amount
          for(;strlen($amount);$amount=substr($amount,0,-2)) // Now loop through each 2 digits of remaining integer part
              $amt=substr ($amount,-2).','.$amt; //forming Indian Currency format by appending (,) for each 2 digits
          echo $sign.$amt; //Appending sign
          

          【讨论】:

          • 请对您的回答进行解释。仅代码答案不受欢迎。
          【解决方案11】:

          我认为这是一个快速而简单的解决方案:-

                  function formatToInr($number){
                  $number=round($number,2);
                  // windows is not supported money_format
              if(setlocale(LC_MONETARY, 'en_IN')){
                   return money_format('%!'.$decimal.'n', $number);
                   }
               else {
                  if(floor($number) == $number) {
                      $append='.00';
                  }else{
                      $append='';
                  }
                  $number = preg_replace("/(\d+?)(?=(\d\d)+(\d)(?!\d))(\.\d+)?/i", "$1,", $number);
                  return $number.$append;
                  }
              }
          

          【讨论】:

            【解决方案12】:

            你应该检查 number_format 函数。Here is the link

            用逗号分隔数千将看起来像

            $rupias = number_format($number, 2, ',', ',');
            

            【讨论】:

            • 我已经检查过了。如何使用印度货币格式的money_format()?
            【解决方案13】:

            我对 money_format() 使用了不同的格式参数来输出。

            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
            

            【讨论】:

              【解决方案14】:

              以上函数不适用于十进制

              $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]; 
                      }
                  }
              

              答案:10,00,00,34,000.001

              【讨论】:

                【解决方案15】:

                完成任务是我自己的职责

                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;
                }
                

                每千次查询需要 0.0110 秒,而 number_format 只需要 0.001 秒。 仅当性能是目标问题时,始终尝试使用 PHP 原生函数。

                【讨论】:

                  【解决方案16】:
                  $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];
                  

                  【讨论】:

                    【解决方案17】:

                    如果你不想在我的情况下使用任何内置函数,我在 iis 服务器上做所以无法使用 php 中的一个函数,所以这样做

                    $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;
                    }
                    

                    【讨论】:

                      【解决方案18】:

                      这适用于整数和浮点值

                          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;
                      
                          }
                      

                      【讨论】:

                        【解决方案19】:

                        使用此功能:

                        function addCommaToRs($amt, &$ret, $dec='', $sign=''){
                            if(preg_match("/-/",$amt)){
                                $amts=explode('-',$amt);
                                $amt=$amts['1'];
                                static $sign='-';
                            } 
                            if(preg_match("/\./",$amt)){
                                $amts=explode('.',$amt);
                                $amt=$amts['0'];
                                $l=strlen($amt);
                                static $dec;
                                $dec=$amts['1'];
                            } else {
                                $l=strlen($amt);
                            }
                            if($l>3){
                                if($l%2==0){
                                    $ret.= substr($amt,0,1);
                                    $ret.= ",";
                                    addCommaToRs(substr($amt,1,$l),$ret,$dec);
                                } else{
                                    $ret.=substr($amt,0,2);
                                    $ret.= ",";     
                                    addCommaToRs(substr($amt,2,$l),$ret,$dec);
                                }
                            } else {
                                $ret.= $amt;
                                if($dec) $ret.=".".$dec;
                            }
                            return $sign.$ret; 
                        }
                        

                        这样称呼它:

                        $amt = '';
                        echo addCommaToRs(123456789.123,&$amt,0);
                        

                        这将返回12,34,567.123

                        【讨论】:

                          【解决方案20】:
                          <?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);
                                              }
                                          }
                                      }
                                  }
                              }
                          ?>
                          

                          【讨论】:

                            【解决方案21】:

                            这是你可以做的简单的事情,

                             float amount = 100000;
                            
                             NumberFormat formatter = NumberFormat.getCurrencyInstance(new Locale("en", "IN"));
                            
                             String moneyString = formatter.format(amount);
                            
                             System.out.println(moneyString);
                            

                            输出将为 100,000.00 卢比。

                            【讨论】:

                            • 不是 PHP 的问题吗?
                            【解决方案22】:
                            declare @Price decimal(26,7)
                            Set @Price=1234456677
                            select FORMAT(@Price,  'c', 'en-In')
                            

                            结果:

                            1,23,44,56,677.00
                            

                            【讨论】:

                              猜你喜欢
                              • 2011-07-19
                              • 2018-03-23
                              • 2012-01-22
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2015-07-24
                              • 1970-01-01
                              相关资源
                              最近更新 更多