4

Hello everyone and thanks for reading . I was wondering how i could format a number into a currency or just simply attach € at the end. I am working in a gridview on the admin page in the yii framework. i have this as eg

'columns'=>array(
        'title',
            array(
                'name'=>'cost',
                'value'=>'$data->cost',
            )
Gunnit
  • 1,064
  • 5
  • 22
  • 45

3 Answers3

8

The Yii way would be to use Yii's existing currency formatter, like this:

array(
      'name'=>'cost',
      'value'=>'Yii::app()->numberFormatter->formatCurrency($data->cost, "EUR")',
     )
adamors
  • 2,672
  • 5
  • 32
  • 45
2

You can use php functions and some static text in value. Example:

'columns'=>array(
        'title',
            array(
                'name'=>'cost',
                'value'=>'$data->cost . " €" ',
            )

If you need to use this kind of formatting even more, I suggest to write a custom function to controller or extend CFormatter with custom currency function and use it as value. Please ead the yii homepage manual below. http://www.yiiframework.com/wiki/278/cgridview-render-customized-complex-datacolumns/

Mihkel Viilveer
  • 432
  • 2
  • 10
1

I use php's money_format() to get the correct formatting.

It could look like this:

'columns'=>array(
    'title',
        array(
            'name'=>'cost',
            'value'=>'money_format("%!i", $data->cost)',
        )

You can specify a currency by setting locale before calling money_format(), like this:

setlocale(LC_MONETARY, 'ja_JP');

ja_JP is for japanese yen, used just as an example. More here.

Community
  • 1
  • 1
ippi
  • 9,857
  • 2
  • 39
  • 50
  • I call it in my SiteController. public function init(){ setlocale(LC_MONETARY, 'ja_JP'); } but you can call it pretty much anywhere as long as it's executed before money_format(). Personally, I think this *should* be set by YII when you set another language in your config. – ippi May 13 '13 at 09:56
  • This solution works on the server, but if you use Windows on the localhost , than you receive "Fatal error: Call to undefined function money_format()". See e.g.: http://stackoverflow.com/questions/6369887/alternative-to-money-format-function-in-php-on-windows-platform – Antonín Slejška Mar 04 '16 at 13:11