diff --git a/utils/number-utils.class.ts b/utils/number-utils.class.ts index 5abe2ce5..064f0c05 100644 --- a/utils/number-utils.class.ts +++ b/utils/number-utils.class.ts @@ -1,38 +1,44 @@ +export enum Level { + NONE, + K, + M, + B +} + + export interface NumberSize { number: number; - size: "B" | "M" | "K" | ""; + size: "B" | "M" | "K" | "" count: number; } -export class NumberUtils{ +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; + public static roundNumber(num: number, level: Level = Level.NONE): any { + var roundNum: NumberSize; + var initialNum = num; + if (num >= 1000000000 && level <= Level.B) { + num = num / 1000000; + num = Math.round(num * 10) / 10; + roundNum = {number: num, size: "B", count: initialNum}; + } else if (num >= 1000000 && level <= Level.M) { + num = num / 1000000; + num = Math.round(num * 10) / 10; + roundNum = {number: num, size: "M", count: initialNum}; + } else if (num >= 1000 && level <= Level.K) { + num = num / 1000; + num = Math.round(num * 10) / 10; + roundNum = {number: num, size: "K", count: initialNum}; + } else if (num >= 100) { + num = num / 100; + num = Math.round(num * 10) / 10; + num = num * 100; + roundNum = {number: num, size: "", count: initialNum}; + } else { + roundNum = {number: num, size: "", count: initialNum}; + } + return roundNum; } - - - + + } diff --git a/utils/pipes/number-round.pipe.ts b/utils/pipes/number-round.pipe.ts index d714f472..f84ee548 100644 --- a/utils/pipes/number-round.pipe.ts +++ b/utils/pipes/number-round.pipe.ts @@ -1,18 +1,15 @@ 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"; @Pipe({name: 'numberRound'}) export class NumberRoundPipe implements PipeTransform { decimalPipe: DecimalPipe = new DecimalPipe("en"); - - constructor() { - - } + constructor() {} 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; } }