@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.
@@ -252,13 +252,7 @@
252
252
  */
253
253
  function toRawFixed(x, minFraction, maxFraction) {
254
254
  var f = maxFraction;
255
- var n;
256
- {
257
- var exactSolve = x * Math.pow(10, f);
258
- var roundDown = Math.floor(exactSolve);
259
- var roundUp = Math.ceil(exactSolve);
260
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
261
- }
255
+ var n = Math.round(x * Math.pow(10, f));
262
256
  var xFinal = n / Math.pow(10, f);
263
257
  // n is a positive integer, but it is possible to be greater than 1e21.
264
258
  // In such case we will go the slow path.
@@ -269,13 +263,9 @@
269
263
  }
270
264
  else {
271
265
  m = n.toString();
272
- var idx1 = m.indexOf('.');
273
- var idx2 = m.indexOf('e+');
274
- var exponent = parseInt(m.substring(idx2 + 2), 10);
275
- m =
276
- m.substring(0, idx1) +
277
- m.substring(idx1 + 1, idx2) +
278
- repeat('0', exponent - (idx2 - idx1 - 1));
266
+ var _a = m.split('e'), mantissa = _a[0], exponent = _a[1];
267
+ m = mantissa.replace('.', '');
268
+ m = m + repeat('0', Math.max(+exponent - m.length + 1, 0));
279
269
  }
280
270
  var int;
281
271
  if (f !== 0) {
@@ -315,23 +305,38 @@
315
305
  xFinal = 0;
316
306
  }
317
307
  else {
318
- e = getMagnitude(x);
319
- var n = void 0;
320
- {
321
- var magnitude = e - p + 1;
322
- var exactSolve =
323
- // Preserve floating point precision as much as possible with multiplication.
324
- magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
325
- var roundDown = Math.floor(exactSolve);
326
- var roundUp = Math.ceil(exactSolve);
327
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
308
+ var xToString = x.toString();
309
+ // If xToString is formatted as scientific notation, the number is either very small or very
310
+ // large. If the precision of the formatted string is lower that requested max precision, we
311
+ // should still infer them from the formatted string, otherwise the formatted result might have
312
+ // precision loss (e.g. 1e41 will not have 0 in every trailing digits).
313
+ var xToStringExponentIndex = xToString.indexOf('e');
314
+ var _a = xToString.split('e'), xToStringMantissa = _a[0], xToStringExponent = _a[1];
315
+ var xToStringMantissaWithoutDecimalPoint = xToStringMantissa.replace('.', '');
316
+ if (xToStringExponentIndex >= 0 &&
317
+ xToStringMantissaWithoutDecimalPoint.length <= p) {
318
+ e = +xToStringExponent;
319
+ m =
320
+ xToStringMantissaWithoutDecimalPoint +
321
+ repeat('0', p - xToStringMantissaWithoutDecimalPoint.length);
322
+ xFinal = x;
323
+ }
324
+ else {
325
+ e = getMagnitude(x);
326
+ var decimalPlaceOffset = e - p + 1;
327
+ // n is the integer containing the required precision digits. To derive the formatted string,
328
+ // we will adjust its decimal place in the logic below.
329
+ var n = Math.round(adjustDecimalPlace(x, decimalPlaceOffset));
330
+ // The rounding caused the change of magnitude, so we should increment `e` by 1.
331
+ if (adjustDecimalPlace(n, p - 1) >= 10) {
332
+ e = e + 1;
333
+ // Divide n by 10 to swallow one precision.
334
+ n = Math.floor(n / 10);
335
+ }
336
+ m = n.toString();
337
+ // Equivalent of n * 10 ** (e - p + 1)
338
+ xFinal = adjustDecimalPlace(n, p - 1 - e);
328
339
  }
329
- // See: https://tc39.es/ecma262/#sec-numeric-types-number-tostring
330
- // No need to worry about scientific notation because it only happens for values >= 1e21,
331
- // which has 22 significant digits. So it will at least be divided by 10 here to bring the
332
- // value back into non-scientific-notation range.
333
- m = n.toString();
334
- xFinal = n * Math.pow(10, (e - p + 1));
335
340
  }
336
341
  var int;
337
342
  if (e >= p - 1) {
@@ -357,6 +362,10 @@
357
362
  }
358
363
  }
359
364
  return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
365
+ // x / (10 ** magnitude), but try to preserve as much floating point precision as possible.
366
+ function adjustDecimalPlace(x, magnitude) {
367
+ return magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
368
+ }
360
369
  }
361
370
  function repeat(s, times) {
362
371
  if (typeof s.repeat === 'function') {
@@ -4692,6 +4701,18 @@
4692
4701
  var polyfill = createCommonjsModule(function (module, exports) {
4693
4702
  Object.defineProperty(exports, "__esModule", { value: true });
4694
4703
 
4704
+ if (typeof Intl === 'undefined') {
4705
+ if (typeof window !== 'undefined') {
4706
+ Object.defineProperty(window, 'Intl', {
4707
+ value: {},
4708
+ });
4709
+ }
4710
+ else if (typeof commonjsGlobal !== 'undefined') {
4711
+ Object.defineProperty(commonjsGlobal, 'Intl', {
4712
+ value: {},
4713
+ });
4714
+ }
4715
+ }
4695
4716
  if (!('getCanonicalLocales' in Intl) ||
4696
4717
  // Native Intl.getCanonicalLocales is just buggy
4697
4718
  Intl.getCanonicalLocales('und-x-private')[0] === 'x-private') {