Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 - Currency symbol position

Tags:

php

yii2

In Yii2 I'm using the function Yii::$app->formatter->asCurrency($sellingPrice); to print currencies. As the locale is Danish the output for this is something like

10,00 kr

Now I've been trying to use this same function to display

kr 10,00 

but I cant find how to do this.

The documentation's directions are to set those options through $numberFormatterOptions and $numberFormatterTextOptions, but no directions as to what exactly to set for those

I tried, among a thousand other things:

Yii::$app->formatter->asCurrency($modifier->getMoneyFormat('sellingPrice'), null, [NumberFormatter::PADDING_POSITION => NumberFormatter::PAD_BEFORE_PREFIX], [NumberFormatter::PADDING_POSITION => NumberFormatter::PAD_BEFORE_PREFIX]) 

but this won't work. I also tried passing [NumberFormatter::PAD_BEFORE_PREFIX => true] to both $numberFormatterOptions and $numberFormatterTextOptions but both wont work.

What am I doing wrong? Or how should I achieve this?

like image 487
mrateb Avatar asked Sep 13 '25 12:09

mrateb


1 Answers

Change Language or 3-letter ISO 4217 currency code:
Example: assuming 'numberFormatterSymbols'=>[\NumberFormatter::CURRENCY_SYMBOL => 'kr'],

Yii::$app->formatter->locale = 'en-US';
echo Yii::$app->formatter->asCurrency(1000); // output: kr1,000.

Yii::$app->formatter->locale = 'ru-RU';
echo Yii::$app->formatter->asCurrency(1000); // output: 1 000 kr.

also for:

\Yii::$app->language = ''; 

Or in the configuration file

$config = [ // or  for locale
   'language' => 'ru_RU',  // Output: 1 000 kr
   'language' => 'en_US',  // Output: kr1,000
    //...
   'components' => [

      'formatter' => [
        'currencyCode' => 'RUB',
        'locale' => 'ru-RU',
        'numberFormatterSymbols'=>[\NumberFormatter::CURRENCY_SYMBOL => 'kr'],
      ],

     //...

The 3-letter ISO 4217 currency code indicating the default currency to use for asCurrency(). If not set, the currency code corresponding to $locale will be used. Note that in this case the $locale has to be specified with a country code, e.g. en-US otherwise it is not possible to determine the default currency.

Note: The Formatter class is meant to be used for formatting values for display to users in different languages and time zones.

like image 79
user206 Avatar answered Sep 16 '25 02:09

user206