Yii::$app->formatter->AsCurrency symbol

Yii::$app->formatter->asCurrency(100, 'EUR') gives me 100 EUR

I expected €100 as a result.
How can I achieve that?

What language string do you use? Do you have intl installed?

at the moment, yii cant do this. But we could implement with 3 lines of code:

// yii\i18n\Formatter
public function asCurrency($value, $currency = null, $options = [], $textOptions = [], $pattern = null)
{
        // ...

        $formatter = $this->createNumberFormatter(NumberFormatter::CURRENCY, null, $options, $textOptions);

        /**
         * allows to implement a custom pattern
         * @see https://www.php.net/manual/de/numberformatter.setpattern.php
         */
        if ($pattern) {
            $formatter->setPattern($pattern);
        }

        // ...
}

echo Yii::$app->formatter->asCurrency(100, 'EUR', [], [], "€#");

Raw PHP code:

Bildschirmfoto%20von%202020-01-20%2009-48-50

@samdark should i create a pull request?

Yes, I have intl installed. The main language is hungarian and I use hu_HU locale.
I also tested de_AT locale and with that worked fine (result: €100).
Maybe this is the expected behaviour, because of the hungarian currency symbol appears after the value?

Looks like so. You can verify it with:

function getCurrencySymbol($currencyCode, $locale = 'en_US')
{
    $formatter = new \NumberFormatter($locale . '@currency=' . $currencyCode, \NumberFormatter::CURRENCY);
    return $formatter->getSymbol(\NumberFormatter::CURRENCY_SYMBOL);
}

echo getCurrencySymbol('EUR', 'hu_HU');
1 Like

Yes, it returns EUR.
Thank you for your help.