2023-01-11 14:35:13 +01:00
|
|
|
export enum Level {
|
|
|
|
NONE,
|
|
|
|
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-01-11 14:51:55 +01:00
|
|
|
public static roundNumber(num: number, level: Level = Level.NONE, decimal = 0): any {
|
|
|
|
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 if (num >= 100) {
|
|
|
|
num = num / 100;
|
2023-01-11 14:51:55 +01:00
|
|
|
num = Math.round(num * variance) / variance;
|
2023-01-11 14:35:13 +01:00
|
|
|
num = num * 100;
|
|
|
|
roundNum = {number: num, size: "", 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
|
|
|
}
|