Gunnit
Gunnit

Reputation: 1074

how to format a column as currency in GridView(yii)?

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',
            )

Upvotes: 4

Views: 9747

Answers (3)

adamors
adamors

Reputation: 2656

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")',
     )

Upvotes: 8

ippi
ippi

Reputation: 10167

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.

Upvotes: 1

Mihkel Viilveer
Mihkel Viilveer

Reputation: 432

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/

Upvotes: 2

Related Questions