7

I want my currency to ignore decimal value, so far I have this:

main.php:

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

],

view:

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

Any idea on how to move forward?

alexandresaiz
  • 2,678
  • 7
  • 29
  • 40
rob180
  • 901
  • 1
  • 9
  • 29

2 Answers2

10

The manual on currencyCode:

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

Try setting currencyCode to 'EUR' (though that 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. Status of the extension can be tested by calling extension_loaded('intl'). In absence of the extension, your best bet is probably to write a custom formatter.

<?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 the default formatter an then call it like this:

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

This also allows you to freely set the currency symbol.

tarleb
  • 19,863
  • 4
  • 51
  • 80
  • i am getting this error `Class 'NumberFormatter' not found` and i have imported `yii\i18n\Formatter` – rob180 Jul 31 '15 at 11:03
  • @rob180 NumberFormatter is a standard [PHP object](http://php.net/manual/en/class.numberformatter.php), I forgot to add some backslashes. Should be fixed now. Please note that one needs to have the PHP intl extensions installed for this to work. – tarleb Jul 31 '15 at 11:57
  • @rob180 You can test if it's available by calling `extension_loaded('intl')`. This should evaluate to `true` if the extensions is present. Unfortunately, the next best alternative seems to be to implement a custom formatter inheriting from `yii\i18n\Formatter`. I'll see if I can put together a minimal class. – tarleb Jul 31 '15 at 13:44
  • @tarleb would you please explain more on writing a custom formatter? I'm not sure where to place the files, what name they should have, etc. I am getting the error 'Unknown format type: roundedCurrency'. – Tom Sep 22 '15 at 18:52
  • OK I got it. It was necessary to specify the custom class in the formatter component configuration: 'class' => 'app\components\Formatter' – Tom Sep 22 '15 at 19:21
3

In main.php:

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

],

Be sure that php_intl extensions is installed. It works for me.

Link to the documentation yii-i18n-formatter.

R13e
  • 1,026
  • 7
  • 12
hesselek
  • 181
  • 1
  • 5