2017-05-28 00:38:50 +02:00
|
|
|
'use strict';
|
2017-08-14 05:01:11 +02:00
|
|
|
var $export = require('./_export');
|
|
|
|
var toInteger = require('./_to-integer');
|
|
|
|
var aNumberValue = require('./_a-number-value');
|
|
|
|
var repeat = require('./_string-repeat');
|
|
|
|
var $toFixed = 1.0.toFixed;
|
|
|
|
var floor = Math.floor;
|
|
|
|
var data = [0, 0, 0, 0, 0, 0];
|
|
|
|
var ERROR = 'Number.toFixed: incorrect invocation!';
|
|
|
|
var ZERO = '0';
|
2017-05-28 00:38:50 +02:00
|
|
|
|
2017-08-14 05:01:11 +02:00
|
|
|
var multiply = function (n, c) {
|
|
|
|
var i = -1;
|
|
|
|
var c2 = c;
|
|
|
|
while (++i < 6) {
|
2017-05-28 00:38:50 +02:00
|
|
|
c2 += n * data[i];
|
|
|
|
data[i] = c2 % 1e7;
|
|
|
|
c2 = floor(c2 / 1e7);
|
|
|
|
}
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var divide = function (n) {
|
|
|
|
var i = 6;
|
|
|
|
var c = 0;
|
|
|
|
while (--i >= 0) {
|
2017-05-28 00:38:50 +02:00
|
|
|
c += data[i];
|
|
|
|
data[i] = floor(c / n);
|
|
|
|
c = (c % n) * 1e7;
|
|
|
|
}
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var numToString = function () {
|
|
|
|
var i = 6;
|
|
|
|
var s = '';
|
|
|
|
while (--i >= 0) {
|
|
|
|
if (s !== '' || i === 0 || data[i] !== 0) {
|
2017-05-28 00:38:50 +02:00
|
|
|
var t = String(data[i]);
|
|
|
|
s = s === '' ? t : s + repeat.call(ZERO, 7 - t.length) + t;
|
|
|
|
}
|
|
|
|
} return s;
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var pow = function (x, n, acc) {
|
2017-05-28 00:38:50 +02:00
|
|
|
return n === 0 ? acc : n % 2 === 1 ? pow(x, n - 1, acc * x) : pow(x * x, n / 2, acc);
|
|
|
|
};
|
2017-08-14 05:01:11 +02:00
|
|
|
var log = function (x) {
|
|
|
|
var n = 0;
|
|
|
|
var x2 = x;
|
|
|
|
while (x2 >= 4096) {
|
2017-05-28 00:38:50 +02:00
|
|
|
n += 12;
|
|
|
|
x2 /= 4096;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
while (x2 >= 2) {
|
|
|
|
n += 1;
|
2017-05-28 00:38:50 +02:00
|
|
|
x2 /= 2;
|
|
|
|
} return n;
|
|
|
|
};
|
|
|
|
|
|
|
|
$export($export.P + $export.F * (!!$toFixed && (
|
|
|
|
0.00008.toFixed(3) !== '0.000' ||
|
|
|
|
0.9.toFixed(0) !== '1' ||
|
|
|
|
1.255.toFixed(2) !== '1.25' ||
|
2017-08-14 05:01:11 +02:00
|
|
|
1000000000000000128.0.toFixed(0) !== '1000000000000000128'
|
|
|
|
) || !require('./_fails')(function () {
|
2017-05-28 00:38:50 +02:00
|
|
|
// V8 ~ Android 4.3-
|
|
|
|
$toFixed.call({});
|
|
|
|
})), 'Number', {
|
2017-08-14 05:01:11 +02:00
|
|
|
toFixed: function toFixed(fractionDigits) {
|
|
|
|
var x = aNumberValue(this, ERROR);
|
|
|
|
var f = toInteger(fractionDigits);
|
|
|
|
var s = '';
|
|
|
|
var m = ZERO;
|
|
|
|
var e, z, j, k;
|
|
|
|
if (f < 0 || f > 20) throw RangeError(ERROR);
|
|
|
|
// eslint-disable-next-line no-self-compare
|
|
|
|
if (x != x) return 'NaN';
|
|
|
|
if (x <= -1e21 || x >= 1e21) return String(x);
|
|
|
|
if (x < 0) {
|
2017-05-28 00:38:50 +02:00
|
|
|
s = '-';
|
|
|
|
x = -x;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (x > 1e-21) {
|
2017-05-28 00:38:50 +02:00
|
|
|
e = log(x * pow(2, 69, 1)) - 69;
|
|
|
|
z = e < 0 ? x * pow(2, -e, 1) : x / pow(2, e, 1);
|
|
|
|
z *= 0x10000000000000;
|
|
|
|
e = 52 - e;
|
2017-08-14 05:01:11 +02:00
|
|
|
if (e > 0) {
|
2017-05-28 00:38:50 +02:00
|
|
|
multiply(0, z);
|
|
|
|
j = f;
|
2017-08-14 05:01:11 +02:00
|
|
|
while (j >= 7) {
|
2017-05-28 00:38:50 +02:00
|
|
|
multiply(1e7, 0);
|
|
|
|
j -= 7;
|
|
|
|
}
|
|
|
|
multiply(pow(10, j, 1), 0);
|
|
|
|
j = e - 1;
|
2017-08-14 05:01:11 +02:00
|
|
|
while (j >= 23) {
|
2017-05-28 00:38:50 +02:00
|
|
|
divide(1 << 23);
|
|
|
|
j -= 23;
|
|
|
|
}
|
|
|
|
divide(1 << j);
|
|
|
|
multiply(1, 1);
|
|
|
|
divide(2);
|
|
|
|
m = numToString();
|
|
|
|
} else {
|
|
|
|
multiply(0, z);
|
|
|
|
multiply(1 << -e, 0);
|
|
|
|
m = numToString() + repeat.call(ZERO, f);
|
|
|
|
}
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
if (f > 0) {
|
2017-05-28 00:38:50 +02:00
|
|
|
k = m.length;
|
|
|
|
m = s + (k <= f ? '0.' + repeat.call(ZERO, f - k) + m : m.slice(0, k - f) + '.' + m.slice(k - f));
|
|
|
|
} else {
|
|
|
|
m = s + m;
|
|
|
|
} return m;
|
|
|
|
}
|
2017-08-14 05:01:11 +02:00
|
|
|
});
|