php Yii2:数字格式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27078178/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Yii2: Number format
提问by Pawan
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.
我已将数据库中的一列定义为浮动。该列在模型中显示为数字。我想将 Gridview 中的 columnn 格式化为两位小数,但找不到方法,如何完成。
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.
谢谢。
回答by Ali MasudianPour
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:
要更熟悉Yii2
的格式,请查看官方文档:
回答by jantbarco
Set grid column like this
像这样设置网格列
'columns' => [
[
'format' => 'Currency',
'attribute' => 'amount',
],
],
in main.php add under 'components':
在 main.php 中的“组件”下添加:
'formatter' => [
'class' => 'yii\i18n\formatter',
'thousandSeparator' => ',',
'decimalSeparator' => '.',
'currencyCode' => '$'
],