@formatjs/intl-numberformat 4.2.1 → 4.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -3,6 +3,49 @@
3
3
  All notable changes to this project will be documented in this file.
4
4
  See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.
5
5
 
6
+ ## [4.2.6](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.5...@formatjs/intl-numberformat@4.2.6) (2020-06-06)
7
+
8
+ **Note:** Version bump only for package @formatjs/intl-numberformat
9
+
10
+
11
+
12
+
13
+
14
+ ## [4.2.5](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.4...@formatjs/intl-numberformat@4.2.5) (2020-06-06)
15
+
16
+ **Note:** Version bump only for package @formatjs/intl-numberformat
17
+
18
+
19
+
20
+
21
+
22
+ ## [4.2.4](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.3...@formatjs/intl-numberformat@4.2.4) (2020-06-04)
23
+
24
+ **Note:** Version bump only for package @formatjs/intl-numberformat
25
+
26
+
27
+
28
+
29
+
30
+ ## [4.2.3](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.2...@formatjs/intl-numberformat@4.2.3) (2020-06-04)
31
+
32
+ **Note:** Version bump only for package @formatjs/intl-numberformat
33
+
34
+
35
+
36
+
37
+
38
+ ## [4.2.2](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.1...@formatjs/intl-numberformat@4.2.2) (2020-06-03)
39
+
40
+
41
+ ### Bug Fixes
42
+
43
+ * **@formatjs/intl-utils:** fix toRawPrecision (round up) and toRawFixed (with huge numbers) ([#1696](https://github.com/formatjs/formatjs/issues/1696)) ([df68427](https://github.com/formatjs/formatjs/commit/df68427c06cd815231ff19b9d5cc493e6df444e2)), closes [#1692](https://github.com/formatjs/formatjs/issues/1692)
44
+
45
+
46
+
47
+
48
+
6
49
  ## [4.2.1](https://github.com/formatjs/formatjs/compare/@formatjs/intl-numberformat@4.2.0...@formatjs/intl-numberformat@4.2.1) (2020-05-28)
7
50
 
8
51
  **Note:** Version bump only for package @formatjs/intl-numberformat
@@ -251,13 +251,7 @@
251
251
  */
252
252
  function toRawFixed(x, minFraction, maxFraction) {
253
253
  const f = maxFraction;
254
- let n;
255
- {
256
- const exactSolve = x * Math.pow(10, f);
257
- const roundDown = Math.floor(exactSolve);
258
- const roundUp = Math.ceil(exactSolve);
259
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
260
- }
254
+ const n = Math.round(x * Math.pow(10, f));
261
255
  const xFinal = n / Math.pow(10, f);
262
256
  // n is a positive integer, but it is possible to be greater than 1e21.
263
257
  // In such case we will go the slow path.
@@ -268,13 +262,9 @@
268
262
  }
269
263
  else {
270
264
  m = n.toString();
271
- const idx1 = m.indexOf('.');
272
- const idx2 = m.indexOf('e+');
273
- const exponent = parseInt(m.substring(idx2 + 2), 10);
274
- m =
275
- m.substring(0, idx1) +
276
- m.substring(idx1 + 1, idx2) +
277
- repeat('0', exponent - (idx2 - idx1 - 1));
265
+ const [mantissa, exponent] = m.split('e');
266
+ m = mantissa.replace('.', '');
267
+ m = m + repeat('0', Math.max(+exponent - m.length + 1, 0));
278
268
  }
279
269
  let int;
280
270
  if (f !== 0) {
@@ -314,23 +304,38 @@
314
304
  xFinal = 0;
315
305
  }
316
306
  else {
317
- e = getMagnitude(x);
318
- let n;
319
- {
320
- const magnitude = e - p + 1;
321
- const exactSolve =
322
- // Preserve floating point precision as much as possible with multiplication.
323
- magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
324
- const roundDown = Math.floor(exactSolve);
325
- const roundUp = Math.ceil(exactSolve);
326
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
307
+ const xToString = x.toString();
308
+ // If xToString is formatted as scientific notation, the number is either very small or very
309
+ // large. If the precision of the formatted string is lower that requested max precision, we
310
+ // should still infer them from the formatted string, otherwise the formatted result might have
311
+ // precision loss (e.g. 1e41 will not have 0 in every trailing digits).
312
+ const xToStringExponentIndex = xToString.indexOf('e');
313
+ const [xToStringMantissa, xToStringExponent] = xToString.split('e');
314
+ const xToStringMantissaWithoutDecimalPoint = xToStringMantissa.replace('.', '');
315
+ if (xToStringExponentIndex >= 0 &&
316
+ xToStringMantissaWithoutDecimalPoint.length <= p) {
317
+ e = +xToStringExponent;
318
+ m =
319
+ xToStringMantissaWithoutDecimalPoint +
320
+ repeat('0', p - xToStringMantissaWithoutDecimalPoint.length);
321
+ xFinal = x;
322
+ }
323
+ else {
324
+ e = getMagnitude(x);
325
+ const decimalPlaceOffset = e - p + 1;
326
+ // n is the integer containing the required precision digits. To derive the formatted string,
327
+ // we will adjust its decimal place in the logic below.
328
+ let n = Math.round(adjustDecimalPlace(x, decimalPlaceOffset));
329
+ // The rounding caused the change of magnitude, so we should increment `e` by 1.
330
+ if (adjustDecimalPlace(n, p - 1) >= 10) {
331
+ e = e + 1;
332
+ // Divide n by 10 to swallow one precision.
333
+ n = Math.floor(n / 10);
334
+ }
335
+ m = n.toString();
336
+ // Equivalent of n * 10 ** (e - p + 1)
337
+ xFinal = adjustDecimalPlace(n, p - 1 - e);
327
338
  }
328
- // See: https://tc39.es/ecma262/#sec-numeric-types-number-tostring
329
- // No need to worry about scientific notation because it only happens for values >= 1e21,
330
- // which has 22 significant digits. So it will at least be divided by 10 here to bring the
331
- // value back into non-scientific-notation range.
332
- m = n.toString();
333
- xFinal = n * Math.pow(10, (e - p + 1));
334
339
  }
335
340
  let int;
336
341
  if (e >= p - 1) {
@@ -356,6 +361,10 @@
356
361
  }
357
362
  }
358
363
  return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
364
+ // x / (10 ** magnitude), but try to preserve as much floating point precision as possible.
365
+ function adjustDecimalPlace(x, magnitude) {
366
+ return magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
367
+ }
359
368
  }
360
369
  function repeat(s, times) {
361
370
  if (typeof s.repeat === 'function') {
@@ -4656,6 +4665,18 @@
4656
4665
  var polyfill = createCommonjsModule(function (module, exports) {
4657
4666
  Object.defineProperty(exports, "__esModule", { value: true });
4658
4667
 
4668
+ if (typeof Intl === 'undefined') {
4669
+ if (typeof window !== 'undefined') {
4670
+ Object.defineProperty(window, 'Intl', {
4671
+ value: {},
4672
+ });
4673
+ }
4674
+ else if (typeof commonjsGlobal !== 'undefined') {
4675
+ Object.defineProperty(commonjsGlobal, 'Intl', {
4676
+ value: {},
4677
+ });
4678
+ }
4679
+ }
4659
4680
  if (!('getCanonicalLocales' in Intl) ||
4660
4681
  // Native Intl.getCanonicalLocales is just buggy
4661
4682
  Intl.getCanonicalLocales('und-x-private')[0] === 'x-private') {