es.number.to-fixed.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. 'use strict';
  2. var $ = require('../internals/export');
  3. var toInteger = require('../internals/to-integer');
  4. var thisNumberValue = require('../internals/this-number-value');
  5. var repeat = require('../internals/string-repeat');
  6. var fails = require('../internals/fails');
  7. var nativeToFixed = 1.0.toFixed;
  8. var floor = Math.floor;
  9. var pow = function (x, n, acc) {
  10. return n === 0 ? acc : n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc);
  11. };
  12. var log = function (x) {
  13. var n = 0;
  14. var x2 = x;
  15. while (x2 >= 4096) {
  16. n += 12;
  17. x2 /= 4096;
  18. }
  19. while (x2 >= 2) {
  20. n += 1;
  21. x2 /= 2;
  22. } return n;
  23. };
  24. var FORCED = nativeToFixed && (
  25. 0.00008.toFixed(3) !== '0.000' ||
  26. 0.9.toFixed(0) !== '1' ||
  27. 1.255.toFixed(2) !== '1.25' ||
  28. 1000000000000000128.0.toFixed(0) !== '1000000000000000128'
  29. ) || !fails(function () {
  30. // V8 ~ Android 4.3-
  31. nativeToFixed.call({});
  32. });
  33. // `Number.prototype.toFixed` method
  34. // https://tc39.github.io/ecma262/#sec-number.prototype.tofixed
  35. $({ target: 'Number', proto: true, forced: FORCED }, {
  36. // eslint-disable-next-line max-statements
  37. toFixed: function toFixed(fractionDigits) {
  38. var number = thisNumberValue(this);
  39. var fractDigits = toInteger(fractionDigits);
  40. var data = [0, 0, 0, 0, 0, 0];
  41. var sign = '';
  42. var result = '0';
  43. var e, z, j, k;
  44. var multiply = function (n, c) {
  45. var index = -1;
  46. var c2 = c;
  47. while (++index < 6) {
  48. c2 += n * data[index];
  49. data[index] = c2 % 1e7;
  50. c2 = floor(c2 / 1e7);
  51. }
  52. };
  53. var divide = function (n) {
  54. var index = 6;
  55. var c = 0;
  56. while (--index >= 0) {
  57. c += data[index];
  58. data[index] = floor(c / n);
  59. c = (c % n) * 1e7;
  60. }
  61. };
  62. var dataToString = function () {
  63. var index = 6;
  64. var s = '';
  65. while (--index >= 0) {
  66. if (s !== '' || index === 0 || data[index] !== 0) {
  67. var t = String(data[index]);
  68. s = s === '' ? t : s + repeat.call('0', 7 - t.length) + t;
  69. }
  70. } return s;
  71. };
  72. if (fractDigits < 0 || fractDigits > 20) throw RangeError('Incorrect fraction digits');
  73. // eslint-disable-next-line no-self-compare
  74. if (number != number) return 'NaN';
  75. if (number <= -1e21 || number >= 1e21) return String(number);
  76. if (number < 0) {
  77. sign = '-';
  78. number = -number;
  79. }
  80. if (number > 1e-21) {
  81. e = log(number * pow(2, 69, 1)) - 69;
  82. z = e < 0 ? number * pow(2, -e, 1) : number / pow(2, e, 1);
  83. z *= 0x10000000000000;
  84. e = 52 - e;
  85. if (e > 0) {
  86. multiply(0, z);
  87. j = fractDigits;
  88. while (j >= 7) {
  89. multiply(1e7, 0);
  90. j -= 7;
  91. }
  92. multiply(pow(10, j, 1), 0);
  93. j = e - 1;
  94. while (j >= 23) {
  95. divide(1 << 23);
  96. j -= 23;
  97. }
  98. divide(1 << j);
  99. multiply(1, 1);
  100. divide(2);
  101. result = dataToString();
  102. } else {
  103. multiply(0, z);
  104. multiply(1 << -e, 0);
  105. result = dataToString() + repeat.call('0', fractDigits);
  106. }
  107. }
  108. if (fractDigits > 0) {
  109. k = result.length;
  110. result = sign + (k <= fractDigits
  111. ? '0.' + repeat.call('0', fractDigits - k) + result
  112. : result.slice(0, k - fractDigits) + '.' + result.slice(k - fractDigits));
  113. } else {
  114. result = sign + result;
  115. } return result;
  116. }
  117. });