user1496306
user1496306

Reputation:

Yii different model value displayed in cgridview and views

I have values that are stored in raw number form percents (e.g .998) in my generated models.

However, for my views, as well as display in CGRIDview I would like these data points to be multiplied by 100, but retain the same value in the backend database. So in my view the above example should display as 99.8 %

Upvotes: 2

Views: 438

Answers (2)

Dmitrii Korotovskii
Dmitrii Korotovskii

Reputation: 586

Define your CGridView columns the following method:

 'columns' => array(
     // ... fields
     array(
         'name' => 'fieldWithPecent',
         'value' => 'sprintf("%3.1f%%", $data->fieldWithPecent * 100)',
     ),
     // other fields definition
 ),

Upvotes: 1

ernie
ernie

Reputation: 6356

Sounds like you want to use sprintf(). In your view, the code might be something like this:

$a = .998;
print sprintf('%3.1f%%', $a*100);

Upvotes: 1

Related Questions