Chandz
Chandz

Reputation: 1233

Formatting currency based on Indian numbering system in laravel

I used PHP's NumberFormatter. But I am getting the error as 'NumberFormatter not found'. Then I used the Money package, https://packagist.org/packages/money/money but, there is no support for the INR(Indian Currency).

Though I am not much concerned with the rupee symbol with the number, at least I need to format the number; for eg: from 7827894.8 to 78,27,894.80. How can I achieve it?

I am not able to use,

Please help me to achive a correct way of representing the money(either by using any package or by using a custom function) TIA.

Upvotes: 0

Views: 3669

Answers (2)

Sapnesh Naik
Sapnesh Naik

Reputation: 11656

From one of my github gists I wrote a few months ago,

<?php
    function IND_money_format($money){

            $decimal = (string)($money - floor($money));
            $money = floor($money);
            $length = strlen($money);
            $m = '';
            $money = strrev($money);
            for($i=0;$i<$length;$i++){
                if(( $i==3 || ($i>3 && ($i-1)%2==0) )&& $i!=$length){
                    $m .=',';
                }
                $m .=$money[$i];
            }
            $result = strrev($m);
            $decimal = preg_replace("/0\./i", ".", $decimal);
            $decimal = substr($decimal, 0, 3);
            if( $decimal != '0'){
            $result = $result.$decimal;
            }
            return $result;
        }
?>

How to add this as a global helper function in laravel?

1 - Create a Helpers directory for your helper functions in the app directory `mkdir app/Helpers' .

2 - Download the Indian_currency_format.php file and save it to Helpers directory.

3 - In your composer.json add the file to "files" attribute of autoload so that it can be loaded by composer using PSR-4 autoloading (see https://getcomposer.org/doc/04-schema.md#psr-4),

like so:
        "autoload": {
            "classmap": [
                "database/seeds",
                "database/factories"
             ],
             "psr-4": {
                "App\\": "app/"
             },
             "files": [
                "app/Helpers/indian_money_format.php"
             ]
        },

4 - Finally run composer dump-autoload to refresh autoload cache.

Check out my blog post on how to format a number to Indian Rupee format in PHP / Laravel to learn in depth how this function actually works.

Upvotes: 1

bazi
bazi

Reputation: 1876

if you don't want to use any packages for this and yet want to do this in a laravel way, i suggest you do the following. the solution has a blend of pure PHP. i have tested this in laravel 5 and it works well.

first create HelperServiceProvider by running:

php artisan make:provider HelperServiceProvider

then open config/app.php and register the service provider:

'providers' => [

    /* other providers */

    'App\Providers\HelperServiceProvider',

]

now create a folder app\Helpers and in that folder create a file moneyformat.php

<?php

/* moneyformat.php */

    function rupee_format($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.
    }

now you'll be able to display the numbers in your blade files like this :

{{ rupee_format($price) }}

references question1, question2

Upvotes: 0

Related Questions