How to display Currency in Indian Numbering Format in PHP

后端 未结 22 2144
一整个雨季
一整个雨季 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 06:50

    Use this function:

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

    Call it like this:

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

    This will return 12,34,567.123.

提交回复
热议问题