2015-10-08 38 views
0

我使用了PHP的NumberFormatter。但我得到错误'NumberFormatter找不到'。然后我用钱包,https://packagist.org/packages/money/money但是,不支持INR(印度货币)。基于laravel中的印度编号系统格式化货币

虽然我并不是很关心卢比符号与数字,至少我需要格式化数字; 例如:7827894.878,27,894.80。 我该如何实现它?

我不能够使用,

  • 的NumberFormatter(国际包装未检测到,虽然我在php.ini中的注释去掉它)
  • money_format()(如我的是windows系统)
  • 钱/钱包装(没有可用于我的问题函数)

请帮我才达到代表金钱的正确方法(通过使用任何或通过使用定制功能TIA

+1

'number_format(7827894.8,2,“。”,“,”)'检查手册页http://php.net/manual/en/function.number-format.php –

+2

@JulioSoares如果他们格式化数字,如OP的例子(在前3个之后的** 2 **数字组),然后'number_format'不会这样做。 – Kryten

+1

@Kryten你是对的。对不起,我没有注意。我从来没有听说过这样的编号系统。 –

回答

0

如果你不想使用任何软件包,但仍然想以laravel的方式做到这一点,我建议你做下面的事情。该解决方案融合了纯PHP。我已经在laravel 5中测试过了,它运行良好。

首先通过运行创造HelperServiceProvider:

PHP工匠化妆:提供HelperServiceProvider

然后打开配置/ 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) }} 

引用question1question2

0

从我github gists一个我几个月前写的,

<?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 - 在应用程序目录 `mkdir app/Helpers'中为您的帮助函数创建一个Helpers目录。

2 - 下载Indian_currency_format.php文件并将其保存到帮助程序目录。

3 - 在你composer.json添加文件 “文件” 自动加载的属性,以便它可以通过使用作曲家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来刷新自动载入缓存。

查看我的博客文章how to format a number to Indian Rupee format in PHP/Laravel以深入了解此功能的实际工作原理。