Yii2:数字格式

Paw*_*wan 10 php yii2

我在Database中定义了一个列为float.该列在模型中显示为数字.我想格式化Gridview中的columnn有两个小数位,无法找到方法,怎么做.

我试图使用此代码,但得到的错误如 - 未知格式类型:数字

[
   'label' => 'Charges',
   'attribute' => 'category_charges',
   'contentOptions' => ['class' => 'col-lg-1'],
   'format' => ['number',2] 
],
Run Code Online (Sandbox Code Playgroud)

谢谢.

Ali*_*our 18

格式化十进制数的正确语法如下所示:

'format'=>['decimal',2]
Run Code Online (Sandbox Code Playgroud)

所以你的代码应该是:

[
    'label' => 'Charges',
    'attribute' =>'category_charges',
    'contentOptions' => ['class' => 'col-lg-1'],
    'format'=>['decimal',2]
],
Run Code Online (Sandbox Code Playgroud)

要更熟悉Yii2格式化,请查看官方文档:

类yii\i18n\Formatter

  • 阿里 - 伟大的它是完美的 (3认同)

小智 7

像这样设置网格列

'columns' => [
        [
            'format' => 'Currency',
            'attribute' => 'amount',
        ],            
    ],
Run Code Online (Sandbox Code Playgroud)

在main.php中添加'components':

'formatter' => [
        'class' => 'yii\i18n\formatter',
        'thousandSeparator' => ',',
        'decimalSeparator' => '.',
        'currencyCode' => '$'
    ],
Run Code Online (Sandbox Code Playgroud)