23
04
Display currency in indian numbering format using php

Hi in this post i am showing you how to convert the number in Indian money format with PHP. Whenever we using the currency in our PHP application, the formatting of currency is very important. Below example will show you how to format the currency. money_format is inbuilt library function which return the string formatted as a currency. It is one of the easiest method of convert the currency format.

Example - Using money_format function


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


Output


10,00,00,34,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. money_format() is only available in PHP 4.3.0 and above. You must be running an older version.

The following code can be used to convert the number format into Indian currency format using pure PHP code.

Pure PHP Code which work on any PHP version and 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.
}

Output

10,00,00,34,000

By posted on - 23rd Apr 2016

Social Oauth Login

Personalized Map Navigation

Online Image Compression Tool

Image Compression

Advertisement

Recent Posts

Categories