yii2 - 如何设置货币十进制值

rob*_*180 5 php money-format yii2

我希望我的货币忽略小数值,到目前为止我有这个:

main.php:

'formatter' => [
   'class' => 'yii\i18n\Formatter',
   'thousandSeparator' => '.',
   'decimalSeparator' => ',',
   'currencyCode' => '€',

],
Run Code Online (Sandbox Code Playgroud)

视图:

[
   'attribute' => 'Score',
   'format' => 'currency',
],
Run Code Online (Sandbox Code Playgroud)

关于如何前进的任何想法?

tar*_*leb 7

手册currencyCode:

3个字母的ISO 4217货币代码,表示要使用的默认货币

尝试设置currencyCode'EUR'(尽管这似乎不那么重要),并把格式化的数组

[
   'attribute' => 'Score',
   'format' => [
       'currency',
       'EUR',
       [
           \NumberFormatter::MIN_FRACTION_DIGITS => 0,
           \NumberFormatter::MAX_FRACTION_DIGITS => 0,
       ]
    ],
],
Run Code Online (Sandbox Code Playgroud)

这需要安装PHP intl扩展.可以通过调用来测试扩展的状态extension_loaded('intl').如果没有扩展名,最好的办法就是编写自定义格式化程序.

<?php
namespace app\components;

class Formatter extends \yii\i18n\Formatter
{
    public function asRoundedCurrency($value, $currency)
    {
        return $this->asInteger(round($value)) . ' ' . $currency;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用它而不是默认的格式化程序,然后像这样调用它:

[
    'attribute' => 'Score',
    'format' => ['roundedCurrency', 'EUR'],
]
Run Code Online (Sandbox Code Playgroud)

这也允许您自由设置货币符号.