2012-04-06 20 views
19

我對格式化盧比貨幣(印度盧比 - INR)有疑問。如何在PHP中以印度文編碼格式顯示貨幣

例如,下面的數字表示爲:

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()

+0

也許這裏http://php.net/manual/en/function.money-format.php可以找到解決方案。 – lgomezma 2012-04-06 10:56:29

+0

@lgomezma:如何以印度貨幣格式使用money_format()? – 2012-04-06 11:01:48

回答

33

你有這麼多的選擇,但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. 
} 
+0

如果使用Windows,請使用[money_format()]的此函數(http://stackoverflow.com/questions/9974704/cross-platform-money-format-linux-and-windows/10042668#10042668)。 – 2012-04-06 11:19:57

+1

@Somnath Muluk在使用'en_IN'時使用區域設置時沒有給出準確的格式 – Baba 2012-04-06 11:23:03

+0

如果給定的數字是整數(round()函數除外),是否有任何設置在零後移除零? – 2012-04-06 11:25:28

1

所以,如果我正在閱讀這個權利,印度的編號系統將成千上萬的權力分離出來,然後每一百次的權力又分開了嗎?嗯...

也許這樣的事情?

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

您需要對包含小數的數字進行操作。 – 2012-04-06 11:20:01

0

您應該檢查number_format功能。 Here is the link

千分用逗號將看起來像

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

我檢查過了。如何使用印度貨幣格式的money_format()? – 2012-04-06 11:05:16

+0

嘗試number_format(),而不是money_format() – Elena 2012-04-06 11:08:55

+0

這是錯誤的....仔細看看格式..... @helen – Baba 2012-04-06 11:12:36

8

的例子你linked正在利用其可與PHP在intl Extension­Docs的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 
+1

仍然無法在Windows上工作 – Baba 2012-04-06 11:20:12

+0

這適用於Windows,給出的例子實際上已經在Windows上運行,請參見[PHP intl擴展](http://stackoverflow.com/q/1451468/367456)。 – hakre 2012-04-06 11:20:54

+0

如何使用'intl ExtensionDocs:'codeigniter框架庫?我無法使用這個功能。 – 2012-04-07 07:08:57

0

我已經使用不同的格式參數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 
9
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); 
} 

注::它不是在浮點值測試,它僅適用於整數

+0

由於** money_format()**在Windows上不起作用。這對我有用。 – Anon30 2016-05-20 07:44:32

+0

它像一個魅力工作! :) – 2016-12-30 05:00:50

-2

繼承人是簡單的事情ü可以做,

float amount = 100000; 

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

String moneyString = formatter.format(amount); 

System.out.println(moneyString); 

輸出將是,100,000盧比。

+4

是不是問題是針對PHP? – Anji 2013-10-03 11:45:10

0

上述功能不能與十進制

工作
$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

4

檢查這個代碼 的工作100%,與十進制設定 u可以使用

印度盧比格式

123456.789 123.456 123.4 和1,2,3,4,5,6,7,8,9,0.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. 

} 
1
$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 
+0

請給出解釋給你的答案。只有代碼答案不值得讚賞。 – 2014-05-26 11:26:32

+0

解釋代碼更多.. – 2014-05-27 08:04:17

0

這是我自己的函數做

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本地函數。

0

使用此功能:

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

-1
declare @Price decimal(26,7) 
Set @Price=1234456677 
select FORMAT(@Price, 'c', 'en-In') 

結果:

1,23,44,56,677.00 
+0

Sry這將工作在SQL ..不知道PHP – Ramya 2015-02-12 07:40:06

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

如果你不想使用任何內置的功能在我的情況我是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; 
} 
0

這對於整數和浮點數值

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; 

    } 
1

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

} 
+0

簡單,並完成工作。 – Farveaz 2017-10-26 09:17:36

0
<?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); 
        } 
       } 
      } 
     } 
    } 
?> 
0
<?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. 
    } 
?>