es6.number.to-fixed.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. 'use strict';
  2. var $export = require('./_export')
  3. , anInstance = require('./_an-instance')
  4. , toInteger = require('./_to-integer')
  5. , aNumberValue = require('./_a-number-value')
  6. , repeat = require('./_string-repeat')
  7. , $toFixed = 1..toFixed
  8. , floor = Math.floor
  9. , data = [0, 0, 0, 0, 0, 0]
  10. , ERROR = 'Number.toFixed: incorrect invocation!'
  11. , ZERO = '0';
  12. var multiply = function(n, c){
  13. var i = -1
  14. , c2 = c;
  15. while(++i < 6){
  16. c2 += n * data[i];
  17. data[i] = c2 % 1e7;
  18. c2 = floor(c2 / 1e7);
  19. }
  20. };
  21. var divide = function(n){
  22. var i = 6
  23. , c = 0;
  24. while(--i >= 0){
  25. c += data[i];
  26. data[i] = floor(c / n);
  27. c = (c % n) * 1e7;
  28. }
  29. };
  30. var numToString = function(){
  31. var i = 6
  32. , s = '';
  33. while(--i >= 0){
  34. if(s !== '' || i === 0 || data[i] !== 0){
  35. var t = String(data[i]);
  36. s = s === '' ? t : s + repeat.call(ZERO, 7 - t.length) + t;
  37. }
  38. } return s;
  39. };
  40. var pow = function(x, n, acc){
  41. return n === 0 ? acc : n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc);
  42. };
  43. var log = function(x){
  44. var n = 0
  45. , x2 = x;
  46. while(x2 >= 4096){
  47. n += 12;
  48. x2 /= 4096;
  49. }
  50. while(x2 >= 2){
  51. n += 1;
  52. x2 /= 2;
  53. } return n;
  54. };
  55. $export($export.P + $export.F * (!!$toFixed && (
  56. 0.00008.toFixed(3) !== '0.000' ||
  57. 0.9.toFixed(0) !== '1' ||
  58. 1.255.toFixed(2) !== '1.25' ||
  59. 1000000000000000128..toFixed(0) !== '1000000000000000128'
  60. ) || !require('./_fails')(function(){
  61. // V8 ~ Android 4.3-
  62. $toFixed.call({});
  63. })), 'Number', {
  64. toFixed: function toFixed(fractionDigits){
  65. var x = aNumberValue(this, ERROR)
  66. , f = toInteger(fractionDigits)
  67. , s = ''
  68. , m = ZERO
  69. , e, z, j, k;
  70. if(f < 0 || f > 20)throw RangeError(ERROR);
  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. });