es.number.to-fixed.js 3.6 KB

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