[英]yii2 - how to set currency decimal value
我希望我的货币忽略小数值,到目前为止我有这个:
main.php:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'thousandSeparator' => '.',
'decimalSeparator' => ',',
'currencyCode' => '€',
],
视图:
[
'attribute' => 'Score',
'format' => 'currency',
],
关于如何前进的任何想法?
currencyCode
手册 :
3个字母的ISO 4217货币代码,表示要使用的默认货币
尝试将currencyCode
设置为'EUR'
(虽然这似乎并不重要)并将格式化程序放在一个数组中
[
'attribute' => 'Score',
'format' => [
'currency',
'EUR',
[
\NumberFormatter::MIN_FRACTION_DIGITS => 0,
\NumberFormatter::MAX_FRACTION_DIGITS => 0,
]
],
],
这需要安装PHP intl扩展。 可以通过调用extension_loaded('intl')
来测试扩展的状态。 如果没有扩展名,最好的办法就是编写自定义格式化程序。
<?php
namespace app\components;
class Formatter extends \yii\i18n\Formatter
{
public function asRoundedCurrency($value, $currency)
{
return $this->asInteger(round($value)) . ' ' . $currency;
}
}
使用它而不是默认的格式化程序,然后像这样调用它:
[
'attribute' => 'Score',
'format' => ['roundedCurrency', 'EUR'],
]
这也允许您自由设置货币符号。
在main.php中:
'formatter' => [
'class' => 'yii\i18n\Formatter',
'locale' => 'yourLocale', //ej. 'es-ES'
'thousandSeparator' => '.',
'decimalSeparator' => ',',
'currencyCode' => 'EUR',
],
确保安装了php_intl扩展。 这个对我有用。
链接到文档yii-i18n-formatter 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.