Skip to content

Commit 43b2292

Browse files
targosjasnell
authored andcommitted
deps: backport ddb5c2d from V8's upstream
Original commit message: Make NumberFormat use the ICU currency data, fix bug in NumberFormat NumberFormat previously just used a min of 0 digits after the decimal and a max of 3. This CL changes it so that we use the ICU currency data, and set the min and max to the number of numbers after the decimal point for each currency. This CL also fixes a small bug where if the minimum fraction digits is above 3 but the maximum fraction digits isn't set, then it returns with only three numbers after the decimal point. BUG=435465,473104,304722 LOG=Y Review URL: https://codereview.chromium.org/1231613006 Cr-Commit-Position: refs/heads/master@{#29734} PR-URL: #6275 Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent ff79a58 commit 43b2292

5 files changed

Lines changed: 58 additions & 7 deletions

File tree

deps/v8/AUTHORS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Bert Belder <bertbelder@gmail.com>
4444
Burcu Dogan <burcujdogan@gmail.com>
4545
Caitlin Potter <caitpotter88@gmail.com>
4646
Craig Schlenter <craig.schlenter@gmail.com>
47+
Chris Nardi <hichris123@gmail.com>
4748
Christopher A. Taylor <chris@gameclosure.com>
4849
Daniel Andersson <kodandersson@gmail.com>
4950
Daniel James <dnljms@gmail.com>

deps/v8/src/i18n.cc

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,24 @@ icu::DecimalFormat* CreateICUNumberFormat(
258258
#endif
259259

260260
number_format = static_cast<icu::DecimalFormat*>(
261-
icu::NumberFormat::createInstance(icu_locale, format_style, status));
261+
icu::NumberFormat::createInstance(icu_locale, format_style, status));
262+
263+
if (U_FAILURE(status)) {
264+
delete number_format;
265+
return NULL;
266+
}
267+
268+
UErrorCode status_digits = U_ZERO_ERROR;
269+
uint32_t fraction_digits = ucurr_getDefaultFractionDigits(
270+
currency.getTerminatedBuffer(), &status_digits);
271+
if (U_SUCCESS(status_digits)) {
272+
number_format->setMinimumFractionDigits(fraction_digits);
273+
number_format->setMaximumFractionDigits(fraction_digits);
274+
} else {
275+
// Set min & max to default values (previously in i18n.js)
276+
number_format->setMinimumFractionDigits(0);
277+
number_format->setMaximumFractionDigits(3);
278+
}
262279
} else if (style == UNICODE_STRING_SIMPLE("percent")) {
263280
number_format = static_cast<icu::DecimalFormat*>(
264281
icu::NumberFormat::createPercentInstance(icu_locale, status));

deps/v8/src/i18n.js

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,11 +1099,19 @@ function initializeNumberFormat(numberFormat, locales, options) {
10991099
var mnid = getNumberOption(options, 'minimumIntegerDigits', 1, 21, 1);
11001100
defineWEProperty(internalOptions, 'minimumIntegerDigits', mnid);
11011101

1102-
var mnfd = getNumberOption(options, 'minimumFractionDigits', 0, 20, 0);
1103-
defineWEProperty(internalOptions, 'minimumFractionDigits', mnfd);
1102+
var mnfd = options['minimumFractionDigits'];
1103+
var mxfd = options['maximumFractionDigits'];
1104+
if (!IS_UNDEFINED(mnfd) || !internalOptions.style === 'currency') {
1105+
mnfd = getNumberOption(options, 'minimumFractionDigits', 0, 20, 0);
1106+
defineWEProperty(internalOptions, 'minimumFractionDigits', mnfd);
1107+
}
11041108

1105-
var mxfd = getNumberOption(options, 'maximumFractionDigits', mnfd, 20, 3);
1106-
defineWEProperty(internalOptions, 'maximumFractionDigits', mxfd);
1109+
if (!IS_UNDEFINED(mxfd) || !internalOptions.style === 'currency') {
1110+
mnfd = IS_UNDEFINED(mnfd) ? 0 : mnfd;
1111+
fallback_limit = (mnfd > 3) ? mnfd : 3;
1112+
mxfd = getNumberOption(options, 'maximumFractionDigits', mnfd, 20, fallback_limit);
1113+
defineWEProperty(internalOptions, 'maximumFractionDigits', mxfd);
1114+
}
11071115

11081116
var mnsd = options['minimumSignificantDigits'];
11091117
var mxsd = options['maximumSignificantDigits'];
@@ -1157,8 +1165,6 @@ function initializeNumberFormat(numberFormat, locales, options) {
11571165
internalOptions,
11581166
resolved);
11591167

1160-
// We can't get information about number or currency style from ICU, so we
1161-
// assume user request was fulfilled.
11621168
if (internalOptions.style === 'currency') {
11631169
ObjectDefineProperty(resolved, 'currencyDisplay', {value: currencyDisplay,
11641170
writable: true});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// Copyright 2015 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Make sure minimumFractionDigits is honored
6+
7+
var nf = new Intl.NumberFormat("en-us",{ useGrouping: false, minimumFractionDigits: 4});
8+
9+
assertEquals("12345.6789", nf.format(12345.6789));
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2015 the V8 project authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
// Make sure currency formatting is correct (for USD only displays two decimal
6+
// places, for JPY 0, and for EUR 2).
7+
8+
var nf_USD = new Intl.NumberFormat(['en'], {style: 'currency', currency: 'USD'});
9+
10+
assertEquals("$54,306.40", nf_USD.format(parseFloat(54306.4047970)));
11+
12+
var nf_JPY = new Intl.NumberFormat(['ja'], {style: 'currency', currency: 'JPY'});
13+
14+
assertEquals("¥54,306", nf_JPY.format(parseFloat(54306.4047970)));
15+
16+
var nf_EUR = new Intl.NumberFormat(['pt'], {style: 'currency', currency: 'EUR'});
17+
18+
assertEquals("€1.000,00", nf_EUR.format(1000.00));

0 commit comments

Comments
 (0)