2023-01-11 14:35:13 +01:00
|
|
|
export enum Level {
|
2023-04-04 15:48:05 +02:00
|
|
|
ALL,
|
2023-01-11 14:35:13 +01:00
|
|
|
K,
|
|
|
|
M,
|
|
|
|
B
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2021-04-16 20:18:02 +02:00
|
|
|
export interface NumberSize {
|
|
|
|
number: number;
|
2023-01-11 14:35:13 +01:00
|
|
|
size: "B" | "M" | "K" | ""
|
2021-04-16 20:18:02 +02:00
|
|
|
count: number;
|
|
|
|
}
|
2017-12-19 13:53:46 +01:00
|
|
|
|
2023-01-11 14:35:13 +01:00
|
|
|
export class NumberUtils {
|
2021-04-16 20:18:02 +02:00
|
|
|
|
2023-04-04 15:48:05 +02:00
|
|
|
public static roundNumber(num: number | string, level: Level = Level.ALL, decimal = 0): any {
|
|
|
|
num = Number.parseFloat(num.toString());
|
2023-01-11 14:51:55 +01:00
|
|
|
let roundNum: NumberSize;
|
|
|
|
let initialNum = num;
|
|
|
|
let variance = Math.pow(10, decimal);
|
2023-01-11 14:35:13 +01:00
|
|
|
if (num >= 1000000000 && level <= Level.B) {
|
|
|
|
num = num / 1000000;
|
2023-01-11 14:51:55 +01:00
|
|
|
num = Math.round(num * variance) / variance;
|
2023-01-11 14:35:13 +01:00
|
|
|
roundNum = {number: num, size: "B", count: initialNum};
|
|
|
|
} else if (num >= 1000000 && level <= Level.M) {
|
|
|
|
num = num / 1000000;
|
2023-01-11 14:51:55 +01:00
|
|
|
num = Math.round(num * variance) / variance;
|
2023-01-11 14:35:13 +01:00
|
|
|
roundNum = {number: num, size: "M", count: initialNum};
|
|
|
|
} else if (num >= 1000 && level <= Level.K) {
|
|
|
|
num = num / 1000;
|
2023-01-11 14:51:55 +01:00
|
|
|
num = Math.round(num * variance) / variance;
|
2023-01-11 14:35:13 +01:00
|
|
|
roundNum = {number: num, size: "K", count: initialNum};
|
|
|
|
} else {
|
|
|
|
roundNum = {number: num, size: "", count: initialNum};
|
|
|
|
}
|
|
|
|
return roundNum;
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|
2023-01-11 14:35:13 +01:00
|
|
|
|
|
|
|
|
2017-12-19 13:53:46 +01:00
|
|
|
}
|