I have defined a column in Database as float. The column is being shown as number in the model. I want to format the columnn in Gridview with two decimal places and couldn't find a way, how it can be done.
I have tried to use this code but getting the error like - Unknown format type: number
[
'label' => 'Charges',
'attribute' => 'category_charges',
'contentOptions' => ['class' => 'col-lg-1'],
'format' => ['number',2]
],
Thanks.
Set grid column like this
'columns' => [
[
'format' => 'Currency',
'attribute' => 'amount',
],
],
in main.php add under 'components':
'formatter' => [
'class' => 'yii\i18n\formatter',
'thousandSeparator' => ',',
'decimalSeparator' => '.',
'currencyCode' => '$'
],
The correct syntax of formatting decimal number is like below:
'format'=>['decimal',2]
So your code should be:
[
'label' => 'Charges',
'attribute' =>'category_charges',
'contentOptions' => ['class' => 'col-lg-1'],
'format'=>['decimal',2]
],
To be more familiar with Yii2
's formatting take a look at the official document:
Class yii\i18n\Formatter
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With