46 lines
1.2 KiB
TypeScript
46 lines
1.2 KiB
TypeScript
export enum Level {
|
|
NONE,
|
|
K,
|
|
M,
|
|
B
|
|
}
|
|
|
|
|
|
export interface NumberSize {
|
|
number: number;
|
|
size: "B" | "M" | "K" | ""
|
|
count: number;
|
|
}
|
|
|
|
export class NumberUtils {
|
|
|
|
public static roundNumber(num: number, level: Level = Level.NONE, decimal = 0): any {
|
|
let roundNum: NumberSize;
|
|
let initialNum = num;
|
|
let variance = Math.pow(10, decimal);
|
|
if (num >= 1000000000 && level <= Level.B) {
|
|
num = num / 1000000;
|
|
num = Math.round(num * variance) / variance;
|
|
roundNum = {number: num, size: "B", count: initialNum};
|
|
} else if (num >= 1000000 && level <= Level.M) {
|
|
num = num / 1000000;
|
|
num = Math.round(num * variance) / variance;
|
|
roundNum = {number: num, size: "M", count: initialNum};
|
|
} else if (num >= 1000 && level <= Level.K) {
|
|
num = num / 1000;
|
|
num = Math.round(num * variance) / variance;
|
|
roundNum = {number: num, size: "K", count: initialNum};
|
|
} else if (num >= 100) {
|
|
num = num / 100;
|
|
num = Math.round(num * variance) / variance;
|
|
num = num * 100;
|
|
roundNum = {number: num, size: "", count: initialNum};
|
|
} else {
|
|
roundNum = {number: num, size: "", count: initialNum};
|
|
}
|
|
return roundNum;
|
|
}
|
|
|
|
|
|
}
|