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