es.number.to-exponential.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 THROWS_ON_INFINITY_FRACTION = fails(function () {
  29. nativeToExponential(1, Infinity);
  30. }) && fails(function () {
  31. nativeToExponential(1, -Infinity);
  32. });
  33. // Safari <11 && FF <50
  34. var PROPER_NON_FINITE_THIS_CHECK = !fails(function () {
  35. nativeToExponential(Infinity, Infinity);
  36. }) && !fails(function () {
  37. nativeToExponential(NaN, Infinity);
  38. });
  39. var FORCED = !ROUNDS_PROPERLY || !THROWS_ON_INFINITY_FRACTION || !PROPER_NON_FINITE_THIS_CHECK;
  40. // `Number.prototype.toExponential` method
  41. // https://tc39.es/ecma262/#sec-number.prototype.toexponential
  42. $({ target: 'Number', proto: true, forced: FORCED }, {
  43. toExponential: function toExponential(fractionDigits) {
  44. var x = thisNumberValue(this);
  45. if (fractionDigits === undefined) return nativeToExponential(x);
  46. var f = toIntegerOrInfinity(fractionDigits);
  47. if (!$isFinite(x)) return String(x);
  48. // TODO: ES2018 increased the maximum number of fraction digits to 100, need to improve the implementation
  49. if (f < 0 || f > 20) throw $RangeError('Incorrect fraction digits');
  50. if (ROUNDS_PROPERLY) return nativeToExponential(x, f);
  51. var s = '';
  52. var m = '';
  53. var e = 0;
  54. var c = '';
  55. var 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 n = 0;
  69. var w = pow(10, e - f);
  70. n = round(x / w);
  71. if (2 * x >= (2 * n + 1) * w) {
  72. n += 1;
  73. }
  74. if (n >= pow(10, f + 1)) {
  75. n /= 10;
  76. e += 1;
  77. }
  78. m = $String(n);
  79. }
  80. if (f !== 0) {
  81. m = stringSlice(m, 0, 1) + '.' + stringSlice(m, 1);
  82. }
  83. if (e === 0) {
  84. c = '+';
  85. d = '0';
  86. } else {
  87. c = e > 0 ? '+' : '-';
  88. d = $String(abs(e));
  89. }
  90. m += 'e' + c + d;
  91. return s + m;
  92. }
  93. });