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