Number round: Add decimal argument in order to be dynamic

This commit is contained in:
Konstantinos Triantafyllou 2023-01-11 15:51:55 +02:00
parent eddd7e00b9
commit 4cd5bcd12b
2 changed files with 18 additions and 9 deletions

View File

@ -14,24 +14,25 @@ export interface NumberSize {
export class NumberUtils {
public static roundNumber(num: number, level: Level = Level.NONE): any {
var roundNum: NumberSize;
var initialNum = num;
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 * 10) / 10;
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 * 10) / 10;
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 * 10) / 10;
num = Math.round(num * variance) / variance;
roundNum = {number: num, size: "K", count: initialNum};
} else if (num >= 100) {
num = num / 100;
num = Math.round(num * 10) / 10;
num = Math.round(num * variance) / variance;
num = num * 100;
roundNum = {number: num, size: "", count: initialNum};
} else {

View File

@ -8,8 +8,16 @@ export class NumberRoundPipe implements PipeTransform {
constructor() {}
transform(value: number): any {
let size: NumberSize = NumberUtils.roundNumber(value, Level.M);
transform(value: number, ...args: any[]): any {
let level = Level.NONE;
let decimal = 0;
if(args[0]) {
level = args[0];
}
if(args[1]) {
decimal = args[1];
}
let size: NumberSize = NumberUtils.roundNumber(value, level, decimal);
return this.decimalPipe.transform(size.number) + size.size;
}
}