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