@formatjs/intl-numberformat 4.1.0 → 4.2.3

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.
@@ -253,13 +253,7 @@
253
253
  */
254
254
  function toRawFixed(x, minFraction, maxFraction) {
255
255
  var f = maxFraction;
256
- var n;
257
- {
258
- var exactSolve = x * Math.pow(10, f);
259
- var roundDown = Math.floor(exactSolve);
260
- var roundUp = Math.ceil(exactSolve);
261
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
262
- }
256
+ var n = Math.round(x * Math.pow(10, f));
263
257
  var xFinal = n / Math.pow(10, f);
264
258
  // n is a positive integer, but it is possible to be greater than 1e21.
265
259
  // In such case we will go the slow path.
@@ -270,13 +264,9 @@
270
264
  }
271
265
  else {
272
266
  m = n.toString();
273
- var idx1 = m.indexOf('.');
274
- var idx2 = m.indexOf('e+');
275
- var exponent = parseInt(m.substring(idx2 + 2), 10);
276
- m =
277
- m.substring(0, idx1) +
278
- m.substring(idx1 + 1, idx2) +
279
- repeat('0', exponent - (idx2 - idx1 - 1));
267
+ var _a = m.split('e'), mantissa = _a[0], exponent = _a[1];
268
+ m = mantissa.replace('.', '');
269
+ m = m + repeat('0', Math.max(+exponent - m.length + 1, 0));
280
270
  }
281
271
  var int;
282
272
  if (f !== 0) {
@@ -316,23 +306,38 @@
316
306
  xFinal = 0;
317
307
  }
318
308
  else {
319
- e = getMagnitude(x);
320
- var n = void 0;
321
- {
322
- var magnitude = e - p + 1;
323
- var exactSolve =
324
- // Preserve floating point precision as much as possible with multiplication.
325
- magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
326
- var roundDown = Math.floor(exactSolve);
327
- var roundUp = Math.ceil(exactSolve);
328
- n = exactSolve - roundDown < roundUp - exactSolve ? roundDown : roundUp;
309
+ var xToString = x.toString();
310
+ // If xToString is formatted as scientific notation, the number is either very small or very
311
+ // large. If the precision of the formatted string is lower that requested max precision, we
312
+ // should still infer them from the formatted string, otherwise the formatted result might have
313
+ // precision loss (e.g. 1e41 will not have 0 in every trailing digits).
314
+ var xToStringExponentIndex = xToString.indexOf('e');
315
+ var _a = xToString.split('e'), xToStringMantissa = _a[0], xToStringExponent = _a[1];
316
+ var xToStringMantissaWithoutDecimalPoint = xToStringMantissa.replace('.', '');
317
+ if (xToStringExponentIndex >= 0 &&
318
+ xToStringMantissaWithoutDecimalPoint.length <= p) {
319
+ e = +xToStringExponent;
320
+ m =
321
+ xToStringMantissaWithoutDecimalPoint +
322
+ repeat('0', p - xToStringMantissaWithoutDecimalPoint.length);
323
+ xFinal = x;
324
+ }
325
+ else {
326
+ e = getMagnitude(x);
327
+ var decimalPlaceOffset = e - p + 1;
328
+ // n is the integer containing the required precision digits. To derive the formatted string,
329
+ // we will adjust its decimal place in the logic below.
330
+ var n = Math.round(adjustDecimalPlace(x, decimalPlaceOffset));
331
+ // The rounding caused the change of magnitude, so we should increment `e` by 1.
332
+ if (adjustDecimalPlace(n, p - 1) >= 10) {
333
+ e = e + 1;
334
+ // Divide n by 10 to swallow one precision.
335
+ n = Math.floor(n / 10);
336
+ }
337
+ m = n.toString();
338
+ // Equivalent of n * 10 ** (e - p + 1)
339
+ xFinal = adjustDecimalPlace(n, p - 1 - e);
329
340
  }
330
- // See: https://tc39.es/ecma262/#sec-numeric-types-number-tostring
331
- // No need to worry about scientific notation because it only happens for values >= 1e21,
332
- // which has 22 significant digits. So it will at least be divided by 10 here to bring the
333
- // value back into non-scientific-notation range.
334
- m = n.toString();
335
- xFinal = n * Math.pow(10, (e - p + 1));
336
341
  }
337
342
  var int;
338
343
  if (e >= p - 1) {
@@ -358,6 +363,10 @@
358
363
  }
359
364
  }
360
365
  return { formattedString: m, roundedNumber: xFinal, integerDigitsCount: int };
366
+ // x / (10 ** magnitude), but try to preserve as much floating point precision as possible.
367
+ function adjustDecimalPlace(x, magnitude) {
368
+ return magnitude < 0 ? x * Math.pow(10, -magnitude) : x / Math.pow(10, magnitude);
369
+ }
361
370
  }
362
371
  function repeat(s, times) {
363
372
  if (typeof s.repeat === 'function') {
@@ -1919,25 +1928,37 @@
1919
1928
  .map(function (x) { return x.value; })
1920
1929
  .join('');
1921
1930
  };
1922
- // https://github.com/tc39/test262/blob/master/test/intl402/NumberFormat/prototype/format/format-function-name.js
1923
- Object.defineProperty(boundFormat, 'name', {
1924
- configurable: true,
1925
- enumerable: false,
1926
- writable: false,
1927
- value: '',
1928
- });
1931
+ try {
1932
+ // https://github.com/tc39/test262/blob/master/test/intl402/NumberFormat/prototype/format/format-function-name.js
1933
+ Object.defineProperty(boundFormat, 'name', {
1934
+ configurable: true,
1935
+ enumerable: false,
1936
+ writable: false,
1937
+ value: '',
1938
+ });
1939
+ }
1940
+ catch (e) {
1941
+ // In older browser (e.g Chrome 36 like polyfill.io)
1942
+ // TypeError: Cannot redefine property: name
1943
+ }
1929
1944
  internalSlots.boundFormat = boundFormat;
1930
1945
  }
1931
1946
  return boundFormat;
1932
1947
  },
1933
1948
  };
1934
- // https://github.com/tc39/test262/blob/master/test/intl402/NumberFormat/prototype/format/name.js
1935
- Object.defineProperty(formatDescriptor.get, 'name', {
1936
- configurable: true,
1937
- enumerable: false,
1938
- writable: false,
1939
- value: 'get format',
1940
- });
1949
+ try {
1950
+ // https://github.com/tc39/test262/blob/master/test/intl402/NumberFormat/prototype/format/name.js
1951
+ Object.defineProperty(formatDescriptor.get, 'name', {
1952
+ configurable: true,
1953
+ enumerable: false,
1954
+ writable: false,
1955
+ value: 'get format',
1956
+ });
1957
+ }
1958
+ catch (e) {
1959
+ // In older browser (e.g Chrome 36 like polyfill.io)
1960
+ // TypeError: Cannot redefine property: name
1961
+ }
1941
1962
  Object.defineProperty(NumberFormat.prototype, 'format', formatDescriptor);
1942
1963
  // Static properties
1943
1964
  defineProperty(NumberFormat, 'supportedLocalesOf', {