[英]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.