-
Notifications
You must be signed in to change notification settings - Fork 2
/
util.ts
53 lines (47 loc) · 1.15 KB
/
util.ts
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
export function bigIntAbs(n: bigint): bigint {
if (n >= 0n) {
return n;
}
return n * -1n;
}
export function getDecimals(n: number | string): number {
if (isNaN(n as any)) {
throw new Error("InvalidNumber");
}
const [preDec, postDec] = _splitString(n.toString(), ".");
return postDec.length;
}
export function extractExp(n: string): [string, number] {
const [mul, expStr] = _splitString(n, "e");
if (expStr === "") {
return [n, 0];
}
const exp = parseInt(expStr, 10);
if (isNaN(exp)) {
throw new Error("InvalidNumber");
}
return [mul, exp];
}
export function countTrailingZeros(n: bigint, upTo: number): number {
if (n === 0n) {
return 0;
}
let count = 0;
let c = n < 0 ? n * -1n : n;
while (c % 10n === 0n && count < upTo) {
count += 1;
c = c / 10n;
}
return count;
}
function _splitString(input: string, char: string): [string, string] {
const pos = input.indexOf(char);
if (pos === -1) {
return [input, ""];
}
const after = input.substr(pos + 1);
if (after.indexOf(char) !== -1) {
throw new Error("InvalidNumber"); // Multiple occurences
}
return [input.substr(0, pos), after];
}