Skip to content

Commit

Permalink
deps: backport f5e924e from V8's upstream
Browse files Browse the repository at this point in the history
Original commit message:

    Fix user options for fractional digits in Intl.NumberFormatter

    The patch in https://crrev.com/ddb5c2d999c5ee6e31c4a9599bb3ddb293cc3f49
    moved all fractional digit settings to default values due to a coding
    error. These were not even correct default values, and users observed
    errors where percentages were written as "23.0%" instead of "23%".

    This patch fixes the setting propagation when appropriate and it changes
    the default max fractional digits of a percentage to 0, per spec.

    BUG=chromium:544122
    R=mnita,jochen
    CC=hichris123,adamk
    LOG=Y

    Review URL: https://codereview.chromium.org/1420883002

    Cr-Commit-Position: refs/heads/master@{#31468}

PR-URL: #6275
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: James M Snell <jasnell@gmail.com>
  • Loading branch information
targos authored and jasnell committed Apr 20, 2016
1 parent 43b2292 commit 213ab35
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
7 changes: 4 additions & 3 deletions deps/v8/src/i18n.js
Original file line number Diff line number Diff line change
Expand Up @@ -1101,14 +1101,15 @@ function initializeNumberFormat(numberFormat, locales, options) {

var mnfd = options['minimumFractionDigits'];
var mxfd = options['maximumFractionDigits'];
if (!IS_UNDEFINED(mnfd) || !internalOptions.style === 'currency') {
if (!IS_UNDEFINED(mnfd) || internalOptions.style !== 'currency') {
mnfd = getNumberOption(options, 'minimumFractionDigits', 0, 20, 0);
defineWEProperty(internalOptions, 'minimumFractionDigits', mnfd);
}

if (!IS_UNDEFINED(mxfd) || !internalOptions.style === 'currency') {
if (!IS_UNDEFINED(mxfd) || internalOptions.style !== 'currency') {
var min_mxfd = internalOptions.style === 'percent' ? 0 : 3;
mnfd = IS_UNDEFINED(mnfd) ? 0 : mnfd;
fallback_limit = (mnfd > 3) ? mnfd : 3;
fallback_limit = (mnfd > min_mxfd) ? mnfd : min_mxfd;
mxfd = getNumberOption(options, 'maximumFractionDigits', mnfd, 20, fallback_limit);
defineWEProperty(internalOptions, 'maximumFractionDigits', mxfd);
}
Expand Down
52 changes: 50 additions & 2 deletions deps/v8/test/intl/number-format/check-minimum-fraction-digits.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,56 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Make sure minimumFractionDigits is honored
// Make sure minimumFractionDigits and maximumFractionDigits are honored

var nf = new Intl.NumberFormat("en-us",{ useGrouping: false, minimumFractionDigits: 4});
var nf = new Intl.NumberFormat("en-us", { useGrouping: false, minimumFractionDigits: 4, maximumFractionDigits: 8});

assertEquals("12345.6789", nf.format(12345.6789));
assertEquals("12345.678912", nf.format(12345.678912));
assertEquals("12345.6700", nf.format(12345.67));
assertEquals("12345.67891234", nf.format(12345.6789123421));

nf = new Intl.NumberFormat("en-us", { useGrouping: false, minimumFractionDigits: 4, maximumFractionDigits: 8, style: 'percent'});

assertEquals("12345.6789%", nf.format(123.456789));
assertEquals("12345.678912%", nf.format(123.45678912));
assertEquals("12345.6700%", nf.format(123.4567));
assertEquals("12345.67891234%", nf.format(123.456789123421));

nf = new Intl.NumberFormat('en', {minimumFractionDigits: 4, maximumFractionDigits: 8, style: 'currency', currency: 'USD'});

assertEquals("$54,306.404797", nf.format(54306.4047970));
assertEquals("$54,306.4000", nf.format(54306.4));
assertEquals("$54,306.40000001", nf.format(54306.400000011));

// Ensure that appropriate defaults exist when minimum and maximum are not specified

nf = new Intl.NumberFormat("en-us", { useGrouping: false });

assertEquals("12345.679", nf.format(12345.6789));
assertEquals("12345.679", nf.format(12345.678912));
assertEquals("12345.67", nf.format(12345.6700));
assertEquals("12345", nf.format(12345));
assertEquals("12345.679", nf.format(12345.6789123421));

nf = new Intl.NumberFormat("en-us", { useGrouping: false, style: 'percent'});

assertEquals("12346%", nf.format(123.456789));
assertEquals("12346%", nf.format(123.45678912));
assertEquals("12346%", nf.format(123.456700));
assertEquals("12346%", nf.format(123.456789123421));
assertEquals("12345%", nf.format(123.45));

// For currency, the minimum or the maximum can be overwritten individually

nf = new Intl.NumberFormat('en', {minimumFractionDigits: 0, style: 'currency', currency: 'USD'});

assertEquals("$54,306.4", nf.format(54306.4047970));
assertEquals("$54,306.4", nf.format(54306.4));
assertEquals("$54,306", nf.format(54306));

nf = new Intl.NumberFormat('en', {maximumFractionDigits: 3, style: 'currency', currency: 'USD'});

assertEquals("$54,306.405", nf.format(54306.4047970));
assertEquals("$54,306.40", nf.format(54306.4));
assertEquals("$54,306.00", nf.format(54306));

0 comments on commit 213ab35

Please sign in to comment.