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

yii2 - how to set currency decimal value

我希望我的货币忽略十进制值,到目前为止我有这个:

main.php:

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

],

查看:

[
   'attribute' => 'Score',
   'format' => 'currency',
],

对如何前进有任何想法吗?

currencyCode上的manual

The 3-letter ISO 4217 currency code indicating the default currency to use

尝试将 currencyCode 设置为 'EUR'(尽管这似乎并不那么重要)并将格式化程序放入数组中

[
   'attribute' => 'Score',
   'format' => [
       'currency',
       'EUR',
       [
           \NumberFormatter::MIN_FRACTION_DIGITS => 0,
           \NumberFormatter::MAX_FRACTION_DIGITS => 0,
       ]
    ],
],

这需要安装 PHP 国际扩展。可以通过调用 extension_loaded('intl') 来测试分机的状态。在没有扩展的情况下,最好的办法可能是编写自定义格式化程序。

<?php
namespace app\components;

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

使用它代替默认格式化程序,然后像这样调用它:

[
    'attribute' => 'Score',
    'format' => ['roundedCurrency', 'EUR'],
]

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

在main.php中:

'formatter' => [
   'class' => 'yii\i18n\Formatter',
   'locale' => 'yourLocale', //ej. 'es-ES'
   'thousandSeparator' => '.',
   'decimalSeparator' => ',',
   'currencyCode' => 'EUR',

],

确保安装了 php_intl 扩展。它对我有用。

Link 到文档 yii-i18n-formatter.