在laravel中基于印度数字系统格式化货币


Formatting currency based on Indian numbering system in laravel

我使用了PHP的NumberFormatter。但我得到的错误是'NumberFormatter未找到'。然后我使用了Money包 https://packagist.org/packages/money/money,但是,不支持INR(印度货币)。

虽然我不太关心卢比符号与数字,至少我需要格式化的数字;如: 7827894.8 , 78, 27894 .80 。我怎样才能实现它?

我不能使用

  • NumberFormatter (Intl包没有检测到,虽然我从php.ini中取消了注释)
  • money_format()(我的是windows系统)
  • Money/Money Package(我的问题没有可用的功能)

请帮助我实现一个正确的方式来表示钱(无论是通过使用任何或通过使用自定义函数) TIA

来自我几个月前写的github目录,

<?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;
        }
?>

如何将其添加为laravel中的全局辅助函数?

1 -在app目录下为你的helper函数创建一个Helpers目录' mkdir app/Helpers' .

2 -下载Indian_currency_format.php文件并保存到Helpers目录。

3 -在你的作曲家。将该文件添加到autoload的"files"属性中,这样它就可以被composer使用PSR-4自动加载来加载(见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 -最后运行composer dump-autoload刷新自动加载缓存

查看我关于如何在PHP/Laravel中将数字格式化为印度卢比格式的博客文章,以深入了解该函数的实际工作原理。

如果您不想为此使用任何包,但又想以一种新的方式完成此操作,我建议您执行以下操作。该解决方案混合了纯PHP。我已经在laravel 5中测试过了,效果很好。

首先运行:

创建HelperServiceProvider

php artisan make:provider HelperServiceProvider

然后打开config/app.php并注册服务提供商:

'providers' => [
    /* other providers */
    'App'Providers'HelperServiceProvider',
]

现在创建一个文件夹app'Helpers,并在该文件夹中创建一个文件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.
    }

现在你就可以像这样在刀片文件中显示数字了:

{{ rupee_format($price) }}

参考文献question1, question2