如果你不想使用任何软件包,但仍然想以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) }}
引用question1,question2
'number_format(7827894.8,2,“。”,“,”)'检查手册页http://php.net/manual/en/function.number-format.php –
@JulioSoares如果他们格式化数字,如OP的例子(在前3个之后的** 2 **数字组),然后'number_format'不会这样做。 – Kryten
@Kryten你是对的。对不起,我没有注意。我从来没有听说过这样的编号系统。 –