How to display Currency in Indian Numbering Format in PHP?

81,696

Solution 1

You have so many options but money_format can do the trick for you.

Example:

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

Output:

1,00,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.

Pure PHP Implementation - Works 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.
}

Solution 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

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

NOTE:: it is not tested on float values and it suitable for only Integer

Solution 4

The example you've linked is making use of the ICU libraries which are available with PHP in the intl Extension­Docs:

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

Or maybe better fitting in your case:

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

Solution 5

Simply use below function to format in INR.

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

Related videos on Youtube

Somnath Muluk
Author by

Somnath Muluk

If you want my advice/help you can make request on Codementor. You can share your screen and get your problem fixed. If you are looking for Freelancers team for your Projects (Website Development/ Wordpress Development / Android Apps / Ios Apps), then we have 15+ people team who works on freelancing projects. Start conversation at email: [email protected]. I am having experience of 10+ years. My profiles: LinkedIn Carrers Twitter Member of Facebook Developer Heroes (Community made by Facebook)

Updated on July 09, 2022

Comments

  • Somnath Muluk
    Somnath Muluk almost 2 years

    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,000
    10,00,000
    1,00,00,000
    10,00,00,000
    

    Refer Indian Numbering System

    I have to do with it PHP.

    I have saw this question Displaying Currency in Indian Numbering Format. But couldn't able to get it for PHP my problem.

    Update:

    How to use money_format() in indian currency format?

    • lgomezma
      lgomezma about 12 years
      Maybe here php.net/manual/en/function.money-format.php you can find the solution.
    • Somnath Muluk
      Somnath Muluk about 12 years
      @lgomezma: How to use money_format() in indian currency format?
    • Suraj Khanal
      Suraj Khanal about 3 years
      The money_format() is deprecated in php 7.4, Please use NumberFormatter
  • Somnath Muluk
    Somnath Muluk about 12 years
    I have checked that. How to use money_format() in indian currency format?
  • Somnath Muluk
    Somnath Muluk about 12 years
    If using windows then use this function for money_format().
  • Salman A
    Salman A about 12 years
    You need to do something about numbers that contain decimals.
  • hakre
    hakre about 12 years
    That works on windows, the example given has been actually run on windows, see PHP intl extension.
  • Baba
    Baba about 12 years
    @Somnath Muluk that does not give accurate formatting when using locale with en_IN
  • Somnath Muluk
    Somnath Muluk about 12 years
    Is there any settings to remove zeros after zero if given number is whole number(other than round() function)?
  • Baba
    Baba about 12 years
    substr_replace($string ,"",-1); that's the cheat i know ... @Somnath Muluk
  • Somnath Muluk
    Somnath Muluk about 12 years
    By doing settings to function money_format() isn't that possible? I don't want to use any hacky method.
  • Baba
    Baba about 12 years
    What about a PURE PHP implementation using number_format ... see php.net/manual/fr/function.number-format.php
  • Somnath Muluk
    Somnath Muluk about 12 years
    But with moneyFormatIndia(), if number is '10000034000.45' then it gives '1,00,00,03,40,00,.45' with number format instead of giving '10,00,00,34,000.45'. If number is whole then I don't want numbers after decimal otherwise I want it.
  • Somnath Muluk
    Somnath Muluk about 12 years
    How to use intl Extension­Docs: library for codeigniter framework? I am unable to use this functions.
  • hakre
    hakre about 12 years
    @SomnathMuluk: It's just PHP, you use it as-is. If it looks too complicated for you, wrap it into a function of it's own for starters.
  • Somnath Muluk
    Somnath Muluk about 12 years
    I have accepted answer, because it helped me to get towards solution.
  • Anji
    Anji over 10 years
    Isn't question is for PHP?
  • Sulthan Allaudeen
    Sulthan Allaudeen almost 10 years
    Please give explanation to your answer. Code only answers are not appreciated.
  • RN Kushwaha
    RN Kushwaha over 9 years
    Poor function not working with decimals and negative.
  • Saifur Rahman Mohsin
    Saifur Rahman Mohsin over 8 years
    Thanks. After spending over 40 mins trying to figure out why money_format did not work on my Ubuntu 14.04 instance running over nginx on Amazon’s AWS EC2, I settled with this for now. The usual money_format works great on my local homestead machine but couldn’t get the production copy to work. Here’s a gist using money_format and @Baba's code as a fallback for anyone who faces the same issue.
  • Anon30
    Anon30 almost 8 years
    As money_format() doesn't work on Windows. this works for me.
  • Abasaheb Gaware
    Abasaheb Gaware almost 6 years
    this returns wrong output for 5 digit negative value. Ex. for -10000 it returns 0,10,000
  • Abasaheb Gaware
    Abasaheb Gaware almost 6 years
    this returns wrong output for 5 digit negative value. Ex. for -10000 it returns -,10,000. solution for this is=> Add if condition like -> if($money[$i]!="-"){ $m .=','; }
  • Butterfly
    Butterfly about 5 years
    @Baba This is excellent. But one small correction. just before $thecash = $explrestunits.$lastthree; we need to add one more line as $explrestunits = rtrim($explrestunits, ','); otherwise number shows as 88,43,46,.73
  • Farveaz
    Farveaz about 5 years
    I think this should be the fallback method if LC_MONETARY fails.
  • Madhur Bhaiya
    Madhur Bhaiya almost 5 years
    Even if on Linux, you might need to generate the en_IN locale using the following commands in succession: sudo locale-gen en_IN.UTF-8 and then sudo update-locale
  • SuKu
    SuKu almost 5 years
    I am using Windows and linux hosting, this method works on both like charm, this is the best and shortest method possible for converting any number to INR format.
  • Anant - Alive to die
    Anant - Alive to die about 3 years
    Second code giving issue for decimal numbers: 3v4l.org/vYuPC
  • Gopipuli
    Gopipuli over 2 years
    Thank you that works better, but if your number has decimal place and more than 3 after decimal it will give a wrong output, only we have to do is cut the left side of decimal then process and while return decimal result add right side of decimal to it. wonderful thank you very much for the function @Baba