fix: multiply percentages by 100 internally to match FormatJS#44
Merged
fix: multiply percentages by 100 internally to match FormatJS#44
Conversation
jrode
reviewed
Feb 4, 2022
| if ($numberElement->style->parsedOptions->style === NumberFormatOptions::STYLE_PERCENT) { | ||
| $key = $numberElement->value; | ||
| if (is_numeric($values[$key])) { | ||
| $values[$key] *= 100; |
jrode
approved these changes
Feb 4, 2022
|
Code Climate has analyzed commit c847373 and detected 1 issue on this pull request. Here's the issue category breakdown:
The test coverage on the diff in this pull request is 100.0% (80% is the threshold). This pull request will bring the total coverage in the repository to 97.3% (0.0% change). View more on Code Climate. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If the parameter is a percent-style number, then we multiply the value
by 100. This is in keeping with the ECMA-402 draft, which specifies the
Intl.NumberFormatrules. When usingIntl.NumberFormatto formatpercentages, the number must first be multiplied by 100 before any
formatting occurs. See section 15.1.6 of ECMA-402, specifically step
5.b.1
ECMA-402, however, doesn't define an API for MessageFormat, so FormatJS
implements this on their own, using
Intl.NumberFormatto process anynumber parameters it encounters.2 As a result, all number parameters
in ICU message syntax that specify the
::percentstem (i.e., "{0,number, ::percent}") have their values first multiplied by 100 before
formatting them.
This may not be considered a bug in FormatJS, since it is adhering to
the ECMA-402 specification. However, it does not follow the rules for
percentages as programmed in icu4c (the underlying library PHP uses),
so in order to match the expected output of FormatJS, we multiply
percent values by 100 before formatting them.
Oddly enough, PHP's
NumberFormatterhas the same rules, and it usesthe underlying ICU implementation of the number formatter:
While:
So, one could argue this is a bug in the ICU implementation of the
percent number skeleton.
Product requirements and context
How has this been tested?
PR Checklist
Footnotes
https://tc39.es/ecma402/#sec-partitionnumberpattern ↩
https://formatjs.io/docs/core-concepts/icu-syntax/#number-type ↩