es6.number.to-fixed.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. 'use strict';
  2. var $export = require('./_export')
  3. , toInteger = require('./_to-integer')
  4. , aNumberValue = require('./_a-number-value')
  5. , repeat = require('./_string-repeat')
  6. , $toFixed = 1..toFixed
  7. , floor = Math.floor
  8. , data = [0, 0, 0, 0, 0, 0]
  9. , ERROR = 'Number.toFixed: incorrect invocation!'
  10. , ZERO = '0';
  11. var multiply = function(n, c){
  12. var i = -1
  13. , 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. , 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. , 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. , 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..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. , f = toInteger(fractionDigits)
  66. , s = ''
  67. , m = ZERO
  68. , e, z, j, k;
  69. if(f < 0 || f > 20)throw RangeError(ERROR);
  70. if(x != x)return 'NaN';
  71. if(x <= -1e21 || x >= 1e21)return String(x);
  72. if(x < 0){
  73. s = '-';
  74. x = -x;
  75. }
  76. if(x > 1e-21){
  77. e = log(x * pow(2, 69, 1)) - 69;
  78. z = e < 0 ? x * pow(2, -e, 1) : x / pow(2, e, 1);
  79. z *= 0x10000000000000;
  80. e = 52 - e;
  81. if(e > 0){
  82. multiply(0, z);
  83. j = f;
  84. while(j >= 7){
  85. multiply(1e7, 0);
  86. j -= 7;
  87. }
  88. multiply(pow(10, j, 1), 0);
  89. j = e - 1;
  90. while(j >= 23){
  91. divide(1 << 23);
  92. j -= 23;
  93. }
  94. divide(1 << j);
  95. multiply(1, 1);
  96. divide(2);
  97. m = numToString();
  98. } else {
  99. multiply(0, z);
  100. multiply(1 << -e, 0);
  101. m = numToString() + repeat.call(ZERO, f);
  102. }
  103. }
  104. if(f > 0){
  105. k = m.length;
  106. m = s + (k <= f ? '0.' + repeat.call(ZERO, f - k) + m : m.slice(0, k - f) + '.' + m.slice(k - f));
  107. } else {
  108. m = s + m;
  109. } return m;
  110. }
  111. });