Yii2:使用千位分隔符格式化货币

Yii2:使用千位分隔符格式化货币,yii2,separator,phpword,Yii2,Separator,Phpword,我使用我的Yii2和Microsoft word 2016 值数据类型decimal(15,2)当我从我的项目字段下载microsoftwordfile.docx时,值decimal显示10000.00但我需要10000.00 如何配置/编码它们以显示10000.00 这里myController.php public function actionWord($id) { Settings::setTempDir(Yii::getAlias('@webroot').'/temp

我使用我的Yii2和Microsoft word 2016

值数据类型
decimal(15,2)
当我从我的项目字段下载microsoftwordfile.docx时,值decimal显示
10000.00
但我需要
10000.00
如何配置/编码它们以显示
10000.00

这里
myController.php

public function actionWord($id)
    {
     Settings::setTempDir(Yii::getAlias('@webroot').'/temp/');
     $templateProcessor = new TemplateProcessor(Yii::getAlias('@webroot').'/path/to/microsoftwordfile.docx');
     $model = Dataexample::findOne($id);
     $templateProcessor->setValue(
         [
            'amount',
         ],
         [
            $model->amount,
         ]);
         $templateProcessor->saveAs(Yii::getAlias('@webroot').'/path/to/microsoftwordfile.docx'); 
        echo Html::a('download', Url::to(Yii::getAlias('@web').'/path/to/microsoftwordfile.docx'), ['class' => 'btn btn-danger']);
     }

您可以使用它来设置货币的格式,它为您提供

千位分隔符
:显示为千位的字符 分隔符(也称为分组分隔符)在格式化 号码

如果您使用的是
应用程序高级
应用程序/config/main.php
如果
应用程序基本
则转到
应用程序高级
应用程序配置/main.php
并在
组件
数组下添加以下内容

'formatter' => [
     'thousandSeparator' => ',',
     'currencyCode' => 'USD',
],
现在您可以格式化任何给定的数字,如下所示

Yii::$app->formatter->asCurrency(100.25);
//will output 
$100.25

Yii::$app->formatter->asCurrency(1000.25);
//will output
$1,000.25

Yii::$app->formatter->asCurrency(100000.25);
//will output 
$100,000.25
你应该像下面那样改变你的函数

public function actionWord($id)
    {
     Settings::setTempDir(Yii::getAlias('@webroot').'/temp/');
     $templateProcessor = new TemplateProcessor(Yii::getAlias('@webroot').'/path/to/microsoftwordfile.docx');
     $model = Dataexample::findOne($id);
     $templateProcessor->setValue(
         [
            'amount',
         ],
         [
            Yii::$app->formatter->asCurrency($model->amount),
         ]);
         $templateProcessor->saveAs(Yii::getAlias('@webroot').'/path/to/microsoftwordfile.docx'); 
        echo Html::a('download', Url::to(Yii::getAlias('@web').'/path/to/microsoftwordfile.docx'), ['class' => 'btn btn-danger']);
     }

希望这能有所帮助。

非常感谢,先生。你的解释很清楚,但我用了这个
Yii::$app->formatter->asDecimal($model->amount,2,[\NumberFormatter::MIN\u FRACTION\u DIGITS=>0]),
因为我不想显示符号货币代码。是的,如果你不想显示货币符号,你可以使用
asDecimal()
,很乐意帮助@chonjaronchauynoo:)