Number round: Add level of round and round to 1 decimal

This commit is contained in:
Konstantinos Triantafyllou 2023-01-11 15:35:13 +02:00
parent 062f1a23c4
commit eddd7e00b9
2 changed files with 39 additions and 36 deletions

View File

@ -1,38 +1,44 @@
export enum Level {
NONE,
K,
M,
B
}
export interface NumberSize { export interface NumberSize {
number: number; number: number;
size: "B" | "M" | "K" | ""; size: "B" | "M" | "K" | ""
count: number; count: number;
} }
export class NumberUtils{ export class NumberUtils {
public static roundNumber(num: number):any { public static roundNumber(num: number, level: Level = Level.NONE): any {
//console.log("Trying to round number: "+ num); var roundNum: NumberSize;
var roundNum: NumberSize = null; var initialNum = num;
var initialNum = num; if (num >= 1000000000 && level <= Level.B) {
if (num >= 1000000000) { num = num / 1000000;
num = num / 1000000; num = Math.round(num * 10) / 10;
num = Math.round(num); roundNum = {number: num, size: "B", count: initialNum};
roundNum = {number: num, size: "B", count: initialNum}; } else if (num >= 1000000 && level <= Level.M) {
} else if(num >= 1000000) { num = num / 1000000;
num=num/1000000; num = Math.round(num * 10) / 10;
num= Math.round(num); roundNum = {number: num, size: "M", count: initialNum};
roundNum = { number: num, size: "M", count: initialNum}; } else if (num >= 1000 && level <= Level.K) {
}else if( num >= 1000){ num = num / 1000;
num=num/1000; num = Math.round(num * 10) / 10;
num= Math.round(num); roundNum = {number: num, size: "K", count: initialNum};
roundNum = { number: num, size: "K", count: initialNum}; } else if (num >= 100) {
}else if (num >= 100) { num = num / 100;
num=num/100; num = Math.round(num * 10) / 10;
num= Math.round(num); num = num * 100;
num=num*100; roundNum = {number: num, size: "", count: initialNum};
roundNum = { number: num, size: "" , count: initialNum}; } else {
}else{ roundNum = {number: num, size: "", count: initialNum};
roundNum = { number: num, size: "" , count: initialNum}; }
} return roundNum;
return roundNum;
} }
} }

View File

@ -1,18 +1,15 @@
import {Pipe, PipeTransform} from "@angular/core"; import {Pipe, PipeTransform} from "@angular/core";
import {NumberSize, NumberUtils} from "../number-utils.class"; import {Level, NumberSize, NumberUtils} from "../number-utils.class";
import {DecimalPipe} from "@angular/common"; import {DecimalPipe} from "@angular/common";
@Pipe({name: 'numberRound'}) @Pipe({name: 'numberRound'})
export class NumberRoundPipe implements PipeTransform { export class NumberRoundPipe implements PipeTransform {
decimalPipe: DecimalPipe = new DecimalPipe("en"); decimalPipe: DecimalPipe = new DecimalPipe("en");
constructor() {}
constructor() {
}
transform(value: number): any { transform(value: number): any {
let size: NumberSize = NumberUtils.roundNumber(value); let size: NumberSize = NumberUtils.roundNumber(value, Level.M);
return this.decimalPipe.transform(size.number) + size.size; return this.decimalPipe.transform(size.number) + size.size;
} }
} }