-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMathUtils.ts
More file actions
69 lines (58 loc) · 1.83 KB
/
MathUtils.ts
File metadata and controls
69 lines (58 loc) · 1.83 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
export default class MathUtils {
public limit = {
B: 1,
KB: 1024,
MB: 1024 * 1024,
GB: 1024 * 1024 * 1024,
};
public fixed = (number: any): string => {
if (number === null || number === undefined || number === "") { return ''}
return Number(number).toFixed(2);
}
public randomWithinRange = (min: number, max: number): number => {
return Math.floor(Math.random() * (max - min) + min);
}
public getRandomColor = (): string => {
const letters = '0123456789ABCDEF';
let color = '#';
for (let i = 0; i < 6; i++) {
color += letters[Math.floor(Math.random() * 16)];
}
return color;
}
public toInt = (str: string[]) => {
return str.map(s => parseInt(s, 10));
}
public formatByte = (byte: number) => {
const {KB, MB, GB} = this.limit;
let unit = "B";
if (byte > GB) {
unit = "GB";
} else if (byte > MB) {
unit = "MB";
} else if (byte > KB) {
unit = "KB";
}
return `${(byte / this.limit[unit]).toFixed(1)} ${unit}`;
}
public getUniqueNumber = (prefix: string) => {
return prefix + Date.now() + ( (Math.random()*100000).toFixed());
}
public formatMoney = (amount) => {
if (typeof (amount) === 'number') {
return amount.toLocaleString();
}
return 'Invalid Amount';
};
public parseNumber = (value) => {
if(value === '') return NaN;
const number = Number(value);
return Number.isInteger(number) ? number : NaN;
};
public getPercent = (total, percent) => {
return Math.round((percent / 100) * total);
};
public progressPercent = (total, value) => {
return Math.round(((value/total) * 100));
}
}