es6.number.to-fixed.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var $export = require('./_export');
  3. var toInteger = require('./_to-integer');
  4. var aNumberValue = require('./_a-number-value');
  5. var repeat = require('./_string-repeat');
  6. var $toFixed = 1.0.toFixed;
  7. var floor = Math.floor;
  8. var data = [0, 0, 0, 0, 0, 0];
  9. var ERROR = 'Number.toFixed: incorrect invocation!';
  10. var ZERO = '0';
  11. var multiply = function (n, c) {
  12. var i = -1;
  13. var c2 = c;
  14. while (++i < 6) {
  15. c2 += n * data[i];
  16. data[i] = c2 % 1e7;
  17. c2 = floor(c2 / 1e7);
  18. }
  19. };
  20. var divide = function (n) {
  21. var i = 6;
  22. var c = 0;
  23. while (--i >= 0) {
  24. c += data[i];
  25. data[i] = floor(c / n);
  26. c = (c % n) * 1e7;
  27. }
  28. };
  29. var numToString = function () {
  30. var i = 6;
  31. var s = '';
  32. while (--i >= 0) {
  33. if (s !== '' || i === 0 || data[i] !== 0) {
  34. var t = String(data[i]);
  35. s = s === '' ? t : s + repeat.call(ZERO, 7 - t.length) + t;
  36. }
  37. } return s;
  38. };
  39. var pow = function (x, n, acc) {
  40. return n === 0 ? acc : n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc);
  41. };
  42. var log = function (x) {
  43. var n = 0;
  44. var x2 = x;
  45. while (x2 >= 4096) {
  46. n += 12;
  47. x2 /= 4096;
  48. }
  49. while (x2 >= 2) {
  50. n += 1;
  51. x2 /= 2;
  52. } return n;
  53. };
  54. $export($export.P + $export.F * (!!$toFixed && (
  55. 0.00008.toFixed(3) !== '0.000' ||
  56. 0.9.toFixed(0) !== '1' ||
  57. 1.255.toFixed(2) !== '1.25' ||
  58. 1000000000000000128.0.toFixed(0) !== '1000000000000000128'
  59. ) || !require('./_fails')(function () {
  60. // V8 ~ Android 4.3-
  61. $toFixed.call({});
  62. })), 'Number', {
  63. toFixed: function toFixed(fractionDigits) {
  64. var x = aNumberValue(this, ERROR);
  65. var f = toInteger(fractionDigits);
  66. var s = '';
  67. var m = ZERO;
  68. var e, z, j, k;
  69. if (f < 0 || f > 20) throw RangeError(ERROR);
  70. // eslint-disable-next-line no-self-compare
  71. if (x != x) return 'NaN';
  72. if (x <= -1e21 || x >= 1e21) return String(x);
  73. if (x < 0) {
  74. s = '-';
  75. x = -x;
  76. }
  77. if (x > 1e-21) {
  78. e = log(x * pow(2, 69, 1)) - 69;
  79. z = e < 0 ? x * pow(2, -e, 1) : x / pow(2, e, 1);
  80. z *= 0x10000000000000;
  81. e = 52 - e;
  82. if (e > 0) {
  83. multiply(0, z);
  84. j = f;
  85. while (j >= 7) {
  86. multiply(1e7, 0);
  87. j -= 7;
  88. }
  89. multiply(pow(10, j, 1), 0);
  90. j = e - 1;
  91. while (j >= 23) {
  92. divide(1 << 23);
  93. j -= 23;
  94. }
  95. divide(1 << j);
  96. multiply(1, 1);
  97. divide(2);
  98. m = numToString();
  99. } else {
  100. multiply(0, z);
  101. multiply(1 << -e, 0);
  102. m = numToString() + repeat.call(ZERO, f);
  103. }
  104. }
  105. if (f > 0) {
  106. k = m.length;
  107. m = s + (k <= f ? '0.' + repeat.call(ZERO, f - k) + m : m.slice(0, k - f) + '.' + m.slice(k - f));
  108. } else {
  109. m = s + m;
  110. } return m;
  111. }
  112. });