Yii2: Formatter ignores 'numberFormatterOptions' when asCurrency() is used.

Created on 24 Sep 2017  路  9Comments  路  Source: yiisoft/yii2

What steps will reproduce the problem?

$formatter = new \yii\i18n\Formatter([
    'locale' => 'en-US',
    'numberFormatterOptions' => [
        \NumberFormatter::MIN_FRACTION_DIGITS => 0,
        \NumberFormatter::MAX_FRACTION_DIGITS => 2,
    ],
    'thousandSeparator' => ' ',
]);

echo $formatter->asCurrency(1000.5, 'USD');
echo $formatter->asDecimal(1000.5);

What is the expected result?

$1 000.5
1 000.5

What do you get instead?

$1 000.50 <== !!!
1 000.5

numberFormatterOptions are ignored while using asCurrency().
see https://github.com/yiisoft/yii2/issues/12345#issuecomment-331181083

Additional info

| Q | A
| ---------------- | ---
| Yii version | 2.0.12
| PHP version | 5.6
| Intl version | 1.1.0, PECL-3.0.0
| Operating system | Windows 10, CentOS

bug

All 9 comments

What is your ICU Data version?

php -i | grep ICU

Thanks for posting in our issue tracker.
In order to properly assist you, we need additional information:

  • When does the issue occur?
  • What do you see?
  • What was the expected result?
  • Can you supply us with a stacktrace? (optional)
  • Do you have exact code to reproduce it? Maybe a PHPUnit tests that fails? (optional)

Thanks!

_This is an automated comment, triggered by adding the label status:need more info._

CentOS - 58.2 (PECL-3.0.0)
Windows - 57.1 (1.1.0)

this is because fraction digits are overwritten by the currency. If you set currency on the formatter it will set fraction digits dependent on the currency. What is the use case to format currency in a non-standard way? people usually would expect dollars to be rendered as $1,50 and not $1,5.

Correct, but people usually would expect dollars to be rendered as $150 and not $150.00!
Note that such situation is when thousandSeparator is set. When only numberFormatterOptions are set asCurrency() uses them. It's not consistent.

@ptz-nerf

Correct, but people usually would expect dollars to be rendered as $150 and not $150.00!

Not really. I work a lot with indexing prices in online stores and all of them displays all fraction digits even if there are only zeros. The only exception are currencies with a high denomination like RUB - for them you never have any fraction digits.
Returning something like $1 000.5 will be definitely a bug for me.

@rob006 I agree. But we have inconsistency here. There are two normal options:

  • asCurrency() ignores Formatter settings and use only locale settings,
  • does not ignore setting that are explicitly specified.

I don't think that asCurrency() should use numberFormatterOptions from formatter. Actually there are only two valid settings for currencies formatting:

  • Always add fraction digits ($9.99 and $10.00).
  • Add fraction digits only for "non-integer" numbers ($9.99 and $10).

Configuring this by MIN_FRACTION_DIGITS and MAX_FRACTION_DIGITS doesn't make sense.

Hi! I have a same problem in 2.0.16, ICU version is 60.2, PHP version is 7.2.15, OS Ubuntu 18,04.
echo Yii::$app->formatter->asCurrency(2000, 'USD'); // "2 000,00聽$", but except "2 000聽$"

P.S. If i not set 'decimalSeparator' and 'thousandSeparator' fields, this method works fine.

Was this page helpful?
0 / 5 - 0 ratings