Node-Red configuration
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

es.number.to-exponential.js 2.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var uncurryThis = require('../internals/function-uncurry-this');
  4. var toIntegerOrInfinity = require('../internals/to-integer-or-infinity');
  5. var thisNumberValue = require('../internals/this-number-value');
  6. var $repeat = require('../internals/string-repeat');
  7. var log10 = require('../internals/math-log10');
  8. var fails = require('../internals/fails');
  9. var $RangeError = RangeError;
  10. var $String = String;
  11. var $isFinite = isFinite;
  12. var abs = Math.abs;
  13. var floor = Math.floor;
  14. var pow = Math.pow;
  15. var round = Math.round;
  16. var nativeToExponential = uncurryThis(1.0.toExponential);
  17. var repeat = uncurryThis($repeat);
  18. var stringSlice = uncurryThis(''.slice);
  19. // Edge 17-
  20. var ROUNDS_PROPERLY = nativeToExponential(-6.9e-11, 4) === '-6.9000e-11'
  21. // IE11- && Edge 14-
  22. && nativeToExponential(1.255, 2) === '1.25e+0'
  23. // FF86-, V8 ~ Chrome 49-50
  24. && nativeToExponential(12345, 3) === '1.235e+4'
  25. // FF86-, V8 ~ Chrome 49-50
  26. && nativeToExponential(25, 0) === '3e+1';
  27. // IE8-
  28. var throwsOnInfinityFraction = function () {
  29. return fails(function () {
  30. nativeToExponential(1, Infinity);
  31. }) && fails(function () {
  32. nativeToExponential(1, -Infinity);
  33. });
  34. };
  35. // Safari <11 && FF <50
  36. var properNonFiniteThisCheck = function () {
  37. return !fails(function () {
  38. nativeToExponential(Infinity, Infinity);
  39. nativeToExponential(NaN, Infinity);
  40. });
  41. };
  42. var FORCED = !ROUNDS_PROPERLY || !throwsOnInfinityFraction() || !properNonFiniteThisCheck();
  43. // `Number.prototype.toExponential` method
  44. // https://tc39.es/ecma262/#sec-number.prototype.toexponential
  45. $({ target: 'Number', proto: true, forced: FORCED }, {
  46. toExponential: function toExponential(fractionDigits) {
  47. var x = thisNumberValue(this);
  48. if (fractionDigits === undefined) return nativeToExponential(x);
  49. var f = toIntegerOrInfinity(fractionDigits);
  50. if (!$isFinite(x)) return String(x);
  51. // TODO: ES2018 increased the maximum number of fraction digits to 100, need to improve the implementation
  52. if (f < 0 || f > 20) throw new $RangeError('Incorrect fraction digits');
  53. if (ROUNDS_PROPERLY) return nativeToExponential(x, f);
  54. var s = '';
  55. var m, e, c, d;
  56. if (x < 0) {
  57. s = '-';
  58. x = -x;
  59. }
  60. if (x === 0) {
  61. e = 0;
  62. m = repeat('0', f + 1);
  63. } else {
  64. // this block is based on https://gist.github.com/SheetJSDev/1100ad56b9f856c95299ed0e068eea08
  65. // TODO: improve accuracy with big fraction digits
  66. var l = log10(x);
  67. e = floor(l);
  68. var w = pow(10, e - f);
  69. var n = round(x / w);
  70. if (2 * x >= (2 * n + 1) * w) {
  71. n += 1;
  72. }
  73. if (n >= pow(10, f + 1)) {
  74. n /= 10;
  75. e += 1;
  76. }
  77. m = $String(n);
  78. }
  79. if (f !== 0) {
  80. m = stringSlice(m, 0, 1) + '.' + stringSlice(m, 1);
  81. }
  82. if (e === 0) {
  83. c = '+';
  84. d = '0';
  85. } else {
  86. c = e > 0 ? '+' : '-';
  87. d = $String(abs(e));
  88. }
  89. m += 'e' + c + d;
  90. return s + m;
  91. }
  92. });