es.number.to-fixed.js 3.7 KB

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