yii2 - 如何设置货币十进制值 [英] yii2 - how to set currency decimal value

查看:24
本文介绍了yii2 - 如何设置货币十进制值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望我的货币忽略十进制值,到目前为止我有这个:

main.php:

'formatter' =>['类' =>'yii\i18n\格式化程序','千分隔符' =>'.','decimalSeparator' =>',','货币代码' =>'€',],

查看:

<预><代码>['属性' =>'分数','格式' =>'货币',],

对如何前进有任何想法吗?

解决方案

currencyCode rel="noreferrer">手册:

<块引用>

3 个字母的 ISO 4217 货币代码,指示要使用的默认货币

尝试将 currencyCode 设置为 'EUR'(尽管这似乎并不重要)并将格式化程序放入数组中

<预><代码>['属性' =>'分数','格式' =>['货币','欧元',[\NumberFormatter::MIN_FRACTION_DIGITS =>0,\NumberFormatter::MAX_FRACTION_DIGITS =>0,]],],

这需要安装 PHP intl 扩展.可以通过调用 extension_loaded('intl') 来测试扩展的状态.如果没有扩展程序,最好的办法可能是编写自定义格式化程序.

asInteger(round($value)) .' ' .$货币;}}

使用它代替默认的格式化程序,然后像这样调用它:

<预><代码>['属性' =>'分数','格式' =>['roundedCurrency', 'EUR'],]

这也允许您自由设置货币符号.

I want my currency to ignore decimal value, so far I have this:

main.php:

'formatter' => [
   'class' => 'yii\i18n\Formatter',
   'thousandSeparator' => '.',
   'decimalSeparator' => ',',
   'currencyCode' => '€',

],

view:

[
   'attribute' => 'Score',
   'format' => 'currency',
],

Any idea on how to move forward?

解决方案

The manual on currencyCode:

The 3-letter ISO 4217 currency code indicating the default currency to use

Try setting currencyCode to 'EUR' (though that doesn't seem to be that important) and put the formatter in an array

[
   'attribute' => 'Score',
   'format' => [
       'currency',
       'EUR',
       [
           \NumberFormatter::MIN_FRACTION_DIGITS => 0,
           \NumberFormatter::MAX_FRACTION_DIGITS => 0,
       ]
    ],
],

This requires the PHP intl extension to be installed. Status of the extension can be tested by calling extension_loaded('intl'). In absence of the extension, your best bet is probably to write a custom formatter.

<?php
namespace app\components;

class Formatter extends \yii\i18n\Formatter
{
    public function asRoundedCurrency($value, $currency)
    {
        return $this->asInteger(round($value)) . ' ' . $currency;
    }
}

Use it instead of the default formatter an then call it like this:

[
    'attribute' => 'Score',
    'format' => ['roundedCurrency', 'EUR'],
]

This also allows you to freely set the currency symbol.

这篇关于yii2 - 如何设置货币十进制值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆