Laravel 中的货币格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/46848603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
currency format in laravel
提问by Johnny
I am using currency format in lots of places in my blade files. I am using number_format to show a proper currency format. So it looks like this
我在刀片文件的很多地方都使用了货币格式。我正在使用 number_format 来显示正确的货币格式。所以它看起来像这样
<p>${{ number_format($row->nCashInitialBalance, 2) }}</p> // ,123.00
<p>${{ number_format($row->nCashCalculatedBalance, 2) }}</p> // 0.50
<p>${{ number_format($row->nCashPaymentsReceived, 2) }}</p> // ,341.15
<p>${{ number_format($row->nCardFinalBalance, 2)}}</p> // 4.10
if I don't use it it looks like this
如果我不使用它,它看起来像这样
<p>${{ $row->nCashInitialBalance }}</p> // 23
<p>${{ $row->nCashCalculatedBalance }}</p> // 0.5
<p>${{ $row->nCashPaymentsReceived }}</p> // 41.15
<p>${{ $row->nCardFinalBalance }}</p> // 4.1
Also for the input fields I am using toFixed(2)
in lots of places.
同样对于我toFixed(2)
在很多地方使用的输入字段。
#nDiscount_fixed").val() = parseFloat( $("#nDiscount_fixed").val()).toFixed(2);
Isn't there an easiest way to show all the variables as proper currency format? I have used number_format
and toFixed(2)
almost more then 50 times now.
没有最简单的方法将所有变量显示为正确的货币格式吗?我已经使用number_format
和toFixed(2)
几乎超过50倍了。
回答by Gijs de Jong
You could create a custom Laravel directive. You still need to call that directive at every place you need it but comes with the benefit that if you ever want to change the code (e.g. replace number_format with something else) you only have to update that directive.
您可以创建自定义 Laravel 指令。您仍然需要在每个需要的地方调用该指令,但它带来的好处是,如果您想更改代码(例如,用其他内容替换 number_format),您只需更新该指令。
Example (taken from the docs and updated for your use case) (in your AppServiceProvider
boot
method):
示例(取自文档并针对您的用例进行了更新)(在您的AppServiceProvider
boot
方法中):
Blade::directive('convert', function ($money) {
return "<?php echo number_format($money, 2); ?>";
});
To use in in Blade:
在 Blade 中使用:
@convert($var)
回答by Cédric
You can add a custom Blade directivein the boot()
method in your AppServiceProvider.php
file.
您可以在文件的方法中添加自定义 Blade 指令。boot()
AppServiceProvider.php
For example:
例如:
Blade::directive('money', function ($amount) {
return "<?php echo '$' . number_format($amount, 2); ?>";
});
And in your Blade file, you will just have to use @money()
like this:
在你的 Blade 文件中,你只需要@money()
像这样使用:
@money($yourVariable)
回答by Robert Kehoe
I would most not use a "directive"... I found it cleaner to do the same logic as an accessor on the model.
我最不会使用“指令”......我发现在模型上执行与访问器相同的逻辑更清晰。
public function getAmountAttribute($value)
{
return money_format('$%i', $value);
}
回答by stillatmylinux
If you want to format negative numbers, you'll need to do it this way:
如果你想格式化负数,你需要这样做:
Blade::directive('money', function ($amount) {
return "<?php
if($amount < 0) {
$amount *= -1;
echo '-$' . number_format($amount, 2);
} else {
echo '$' . number_format($amount, 2);
}
?>";
});
In your blade file use:
在您的刀片文件中使用:
@money(-10)
If you make edits to your directive, you'll need to clear your view:
如果您对指令进行编辑,则需要清除视图:
php artisan view:clear