【问题标题】:PHP Money Format in Indian Currency?印度货币的PHP货币格式?
【发布时间】:2012-01-22 22:44:34
【问题描述】:

例如$num='7,57,800';

如何将$number 的值显示为 75.7 万?

【问题讨论】:

  • string money_format (string $format, float $number) 来自 php 手册?
  • 为了澄清:您是否尝试将 7,57,800 转换为 7.57 Lakhs ?或者尝试将757800 转换为7,57,800

标签: php currency money-format number-formatting


【解决方案1】:

函数如下:

function formatInIndianStyle($num){
     $pos = strpos((string)$num, ".");
     if ($pos === false) {
        $decimalpart="00";
     }
     if (!($pos === false)) {
        $decimalpart= substr($num, $pos+1, 2); $num = substr($num,0,$pos);
     }

     if(strlen($num)>3 & strlen($num) <= 12){
         $last3digits = substr($num, -3 );
         $numexceptlastdigits = substr($num, 0, -3 );
         $formatted = makeComma($numexceptlastdigits);
         $stringtoreturn = $formatted.",".$last3digits.".".$decimalpart ;
     }elseif(strlen($num)<=3){
        $stringtoreturn = $num.".".$decimalpart ;
     }elseif(strlen($num)>12){
        $stringtoreturn = number_format($num, 2);
     }

     if(substr($stringtoreturn,0,2)=="-,"){
        $stringtoreturn = "-".substr($stringtoreturn,2 );
     }

     return $stringtoreturn;
 }

 function makeComma($input){ 
     if(strlen($input)<=2)
     { return $input; }
     $length=substr($input,0,strlen($input)-2);
     $formatted_input = makeComma($length).",".substr($input,-2);
     return $formatted_input;
 }

【讨论】:

    【解决方案2】:

    检查这个插件-http://archive.plugins.jquery.com/project/numberformatter

    这是一个如何使用此插件的示例。

     $("#salary").blur(function(){
     $(this).parseNumber({format:"#,###.00", locale:"us"});
    $(this).formatNumber({format:"#,###.00", locale:"us"});
    });
    

    只需更改语言环境..

    更多示例和信息请访问-http://code.google.com/p/jquery-numberformatter/

    我的示例来源:http://code.google.com/p/jquery-numberformatter/

    希望这会有所帮助:)

    【讨论】:

      【解决方案3】:

      这是另一种解决方案,仅供参考:

      <?php
      #    Output easy-to-read numbers
      #    by james at bandit.co.nz
      function bd_nice_number($n) {
          // first strip any formatting;
          $n = (0+str_replace(",","",$n));
      
          // is this a number?
          if(!is_numeric($n)) return false;
      
          // now filter it;
          if($n>1000000000000) return round(($n/1000000000000),1).' trillion';
          else if($n>1000000000) return round(($n/1000000000),1).' billion';
          else if($n>1000000) return round(($n/1000000),1).' million';
          else if($n>1000) return round(($n/1000),1).' thousand';
      
          return number_format($n);
      }
      ?>
      

      【讨论】:

        【解决方案4】:
        <?php //Credits are going to: @Niet-the-Dark-Absol
        
            function indian_number_format($num){
                $num=explode('.',$num);
                $dec=(count($num)==2)?'.'.$num[1]:'.00';
                $num = (string)$num[0];
                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.$dec;
            }
        ?>
        

        问题:stackoverflow.com/questions/10042485

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-10-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2012-01-29
          相关资源
          最近更新 更多