-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFctHelper.js
More file actions
48 lines (48 loc) · 1.64 KB
/
FctHelper.js
File metadata and controls
48 lines (48 loc) · 1.64 KB
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
class FctHelper {
static round(x, n, separator = '.') {
if (n === 0) {
let result = x.toFixed(0);
let i = result.search(/[^0-9-]/);
if (i === -1)
return result;
else return result.substring(0, i);
}
const toStringWithoutTrailingZeroes = (f) => {
const removeTrailingZeros = (s) => {
let i = s.search(/[^0-9-]/);
if (i === -1)
return s;
i = s.search(/0+$/);
return i === -1 ? s : s.substring(0, i);
};
let result = f.toFixed(n + 2);
return removeTrailingZeros(result);
};
let result = toStringWithoutTrailingZeroes(x);
let i = result.search(/[^0-9-]/);
if (i === -1)
return result + separator + '0'.repeat(n);
if (result.length <= n + i + 1) {
if (result[i] !== separator)
result = result.substring(0, i) + separator + result.substring(i + 1);
let missingTrailingZeroes = n + 1 - (result.length - i);
return result + '0'.repeat(missingTrailingZeroes);
}
let d = 0;
if (Math.floor(x * Math.pow(10, n + 1) - 10 * Math.floor(x * Math.pow(10, n))) > 4)
d = 1;
x = (Math.floor(x * Math.pow(10, n)) + d) / Math.pow(10, n);
result = toStringWithoutTrailingZeroes(x);
i = result.search(/[^0-9-]/);
if (i === -1)
return result + separator + '0'.repeat(n);
if (result[i] !== separator)
result = result.substring(0, i) + separator + result.substring(i + 1);
if (result.length <= n + i + 1) {
let missingTrailingZeroes = n + 1 - (result.length - i);
return result + '0'.repeat(missingTrailingZeroes);
}
// the operation did not eliminate all superfluous digits (because IEEE 754 floating-point arithmetic sucks) => just cut
return result.substring(0, n + i + 1);
}
}