Yii2 - how to set the decimal value of a currency

I want the decimal value to be ignored in my currency as long as I have it:

main.php:

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

View:

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

Any idea on how to move forward?

+5
source share
2 answers

currencyCode manual :

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

Try setting currencyCode to 'EUR' (although this doesn't seem to be that important) and put the formatter in an array

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

This requires the PHP intl extension to be installed. The extension status can be checked by calling extension_loaded('intl') . If there is no extension, your best bet is probably to write custom formatting.

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

Use it instead of standard formatting and then call it like this:

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

It also allows you to freely set the currency symbol.

+7
source

In main.php:

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

Make sure php_intl extensions are installed. This works for me.

Link to yii-i18n-formatter documentation .

+3
source

All Articles