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

39 lines
1.0 KiB
TypeScript

export interface NumberSize {
number: number;
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;
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;
}
}