How to display Currency in Indian Numbering Format in PHP?
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 ExtensionDocs:
$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);
}
Related videos on Youtube
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, 2022Comments
-
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 about 12 yearsMaybe here php.net/manual/en/function.money-format.php you can find the solution.
-
Somnath Muluk about 12 years@lgomezma: How to use money_format() in indian currency format?
-
Suraj Khanal about 3 yearsThe money_format() is deprecated in php 7.4, Please use NumberFormatter
-
-
Somnath Muluk about 12 yearsI have checked that. How to use money_format() in indian currency format?
-
Somnath Muluk about 12 yearsIf using windows then use this function for money_format().
-
Salman A about 12 yearsYou need to do something about numbers that contain decimals.
-
hakre about 12 yearsThat works on windows, the example given has been actually run on windows, see PHP intl extension.
-
Baba about 12 years@Somnath Muluk that does not give accurate formatting when using locale with
en_IN
-
Somnath Muluk about 12 yearsIs there any settings to remove zeros after zero if given number is whole number(other than round() function)?
-
Baba about 12 years
substr_replace($string ,"",-1);
that's the cheat i know ... @Somnath Muluk -
Somnath Muluk about 12 yearsBy doing settings to function money_format() isn't that possible? I don't want to use any hacky method.
-
Baba about 12 yearsWhat about a PURE PHP implementation using number_format ... see php.net/manual/fr/function.number-format.php
-
Somnath Muluk about 12 yearsBut 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 about 12 yearsHow to use
intl ExtensionDocs:
library for codeigniter framework? I am unable to use this functions. -
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 about 12 yearsI have accepted answer, because it helped me to get towards solution.
-
Anji over 10 yearsIsn't question is for PHP?
-
Sulthan Allaudeen almost 10 yearsPlease give explanation to your answer. Code only answers are not appreciated.
-
RN Kushwaha over 9 yearsPoor function not working with decimals and negative.
-
Saifur Rahman Mohsin over 8 yearsThanks. 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 almost 8 yearsAs money_format() doesn't work on Windows. this works for me.
-
Abasaheb Gaware almost 6 yearsthis returns wrong output for 5 digit negative value. Ex. for -10000 it returns 0,10,000
-
Abasaheb Gaware almost 6 yearsthis 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 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 as88,43,46,.73
-
Farveaz about 5 yearsI think this should be the fallback method if LC_MONETARY fails.
-
Madhur Bhaiya almost 5 yearsEven 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 thensudo update-locale
-
SuKu almost 5 yearsI 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 about 3 yearsSecond code giving issue for decimal numbers: 3v4l.org/vYuPC
-
Gopipuli over 2 yearsThank 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