Format a number to Indian Rupee format in PHP / Laravel

Format a number to Indian Rupee format in PHP / Laravel

One of the tasks I usually come across while working on some of the more commercial web applications is to display a number in INR i.e Indian National Rupee ( ₹ ) format which makes it really easy to perceive the number as Indian Rupees. Let me show you how to format a number to Indian Rupee format in PHP / Laravel project.

The Indian Rupee Representation uses lakh (1,00,000) and crore (1,00,00,000) so a numerical such as 125520 would be represented as 1,25,520.

This is a function I wrote in PHP for use in my Laravel project but it can be adapted to work in any programming language you like. The function takes a number as Input and returns the result as a string formatted to INR format.

The IND_money_format() function:

What this function actually does:

Here the decimal part($decimal) of the input number is separately stored as a string which we will use later. We then get rid of the decimal part of the number by using the floor() function.  This rounds DOWN a number to the nearest integer.

We need the length of the of the string ($money) which is going to be used in a for loop later. We then reverse the original string and also initialize an empty string ( $delimiter ) . This will hold the delimiter ( , ) value as we loop over the reversed string.

The for loop below is the heart of our function. The loop will run as many times as the length of the string and the if loop inside for loop assigns the delimiter ( , ) to the $delimiter in the cases of:

    • $i = 3 : The first delimiter will always be placed at the 3rd position (from last i.e end of the string).
    • (( $i>3 && ($i-1)%2==0) )&& $i!=$length ) : The subsequent delimiters will be placed after every two characters excluding the last character in the string.
    • $delimiter .=$money[$i] : Remember we revered the string in a previous step?. This builds up a new string from that reversed string adding up delimiters in appropriate positions.

We then restore the reversed string to its original format and then perform a regular expression search to append a dot ( . ) to the beginning of the decimal string.

We also limit the decimal part to only 3 digits ( can be easily changed according to your requirements).

Then simply concatenate the integer part with the decimal part and return the result!!

You can also add this function as a global function in Laravel!! see How to add a Global function in Laravel using Composer?

What do you think? Is there a better way of doing this? Please share your thoughts in the comments below!


You can also consider making a small donation to support me. Your donation will directly contribute to the running cost of this website and hopefully my college too 🙂

Paypal: | UPI: [email protected]

  • 9

Leave a Reply

3 Comment threads
0 Thread replies
Most reacted comment
Hottest comment thread
3 Comment authors
jitendar chauhanVikash Kumar SinghGangaram Chodankar Recent comment authors
newest oldest most voted
Notify of
Gangaram Chodankar

Hi.. the above function is working fine for non negative numbers
so for negative numbers i have added code as below before return result
$firstCharacter = $result[0];
$secondCharacter = $result[1];
if ($firstCharacter == ‘-‘ && $secondCharacter == ‘,’){
$result = substr_replace($result, ”, 1, 1);

Vikash Kumar Singh

Thanks for useful tutorial

jitendar chauhan

This function is not working properly with float number such as 111.1