openaire-library/utils/number-utils.class.ts

39 lines
1.0 KiB
TypeScript
Raw Normal View History

export interface NumberSize {
number: number;
2022-12-21 12:37:16 +01:00
size: "B" | "M" | "K" | "";
count: number;
}
export class NumberUtils{
public static roundNumber(num: number):any {
//console.log("Trying to round number: "+ num);
var roundNum: NumberSize = null;
var initialNum = num;
2022-12-21 12:37:16 +01:00
if (num >= 1000000000) {
num = num / 1000000;
num = Math.round(num);
roundNum = {number: num, size: "B", count: initialNum};
} else if(num >= 1000000) {
num=num/1000000;
num= Math.round(num);
roundNum = { number: num, size: "M", count: initialNum};
}else if( num >= 1000){
num=num/1000;
num= Math.round(num);
roundNum = { number: num, size: "K", count: initialNum};
}else if (num >= 100) {
num=num/100;
num= Math.round(num);
num=num*100;
roundNum = { number: num, size: "" , count: initialNum};
}else{
roundNum = { number: num, size: "" , count: initialNum};
}
return roundNum;
}
}