Number round: Add level of round and round to 1 decimal
This commit is contained in:
parent
062f1a23c4
commit
eddd7e00b9
|
@ -1,30 +1,37 @@
|
|||
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 {
|
||||
|
||||
public static roundNumber(num: number):any {
|
||||
//console.log("Trying to round number: "+ num);
|
||||
var roundNum: NumberSize = null;
|
||||
public static roundNumber(num: number, level: Level = Level.NONE): any {
|
||||
var roundNum: NumberSize;
|
||||
var initialNum = num;
|
||||
if (num >= 1000000000) {
|
||||
if (num >= 1000000000 && level <= Level.B) {
|
||||
num = num / 1000000;
|
||||
num = Math.round(num);
|
||||
num = Math.round(num * 10) / 10;
|
||||
roundNum = {number: num, size: "B", count: initialNum};
|
||||
} else if(num >= 1000000) {
|
||||
} else if (num >= 1000000 && level <= Level.M) {
|
||||
num = num / 1000000;
|
||||
num= Math.round(num);
|
||||
num = Math.round(num * 10) / 10;
|
||||
roundNum = {number: num, size: "M", count: initialNum};
|
||||
}else if( num >= 1000){
|
||||
} else if (num >= 1000 && level <= Level.K) {
|
||||
num = num / 1000;
|
||||
num= Math.round(num);
|
||||
num = Math.round(num * 10) / 10;
|
||||
roundNum = {number: num, size: "K", count: initialNum};
|
||||
} else if (num >= 100) {
|
||||
num = num / 100;
|
||||
num= Math.round(num);
|
||||
num = Math.round(num * 10) / 10;
|
||||
num = num * 100;
|
||||
roundNum = {number: num, size: "", count: initialNum};
|
||||
} else {
|
||||
|
@ -34,5 +41,4 @@ export class NumberUtils{
|
|||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue