Fix number pipe round in order to avoid round in numbers < 1000. Extend pipe to accept number in string format instead of number.
This commit is contained in:
parent
4fa36bdd43
commit
133fdfbff2
|
@ -1,5 +1,5 @@
|
|||
export enum Level {
|
||||
NONE,
|
||||
ALL,
|
||||
K,
|
||||
M,
|
||||
B
|
||||
|
@ -14,7 +14,8 @@ export interface NumberSize {
|
|||
|
||||
export class NumberUtils {
|
||||
|
||||
public static roundNumber(num: number, level: Level = Level.NONE, decimal = 0): any {
|
||||
public static roundNumber(num: number | string, level: Level = Level.ALL, decimal = 0): any {
|
||||
num = Number.parseFloat(num.toString());
|
||||
let roundNum: NumberSize;
|
||||
let initialNum = num;
|
||||
let variance = Math.pow(10, decimal);
|
||||
|
@ -30,11 +31,6 @@ export class NumberUtils {
|
|||
num = num / 1000;
|
||||
num = Math.round(num * variance) / variance;
|
||||
roundNum = {number: num, size: "K", count: initialNum};
|
||||
} else if (num >= 100) {
|
||||
num = num / 100;
|
||||
num = Math.round(num * variance) / variance;
|
||||
num = num * 100;
|
||||
roundNum = {number: num, size: "", count: initialNum};
|
||||
} else {
|
||||
roundNum = {number: num, size: "", count: initialNum};
|
||||
}
|
||||
|
|
|
@ -6,18 +6,23 @@ import {DecimalPipe} from "@angular/common";
|
|||
export class NumberRoundPipe implements PipeTransform {
|
||||
decimalPipe: DecimalPipe = new DecimalPipe("en");
|
||||
|
||||
constructor() {}
|
||||
constructor() {
|
||||
}
|
||||
|
||||
transform(value: number, ...args: any[]): any {
|
||||
let level = Level.NONE;
|
||||
/**
|
||||
* Args: Level: 0 (default): ALL, 1: K, 2: M, 3:B
|
||||
* Decimal: how many decimals should be shown (e.g 1 -> 62.1)
|
||||
* */
|
||||
transform(value: number | string, ...args: any[]): any {
|
||||
let level = Level.ALL;
|
||||
let decimal = 0;
|
||||
if(args[0]) {
|
||||
if (args[0]) {
|
||||
level = args[0];
|
||||
}
|
||||
if(args[1]) {
|
||||
if (args[1]) {
|
||||
decimal = args[1];
|
||||
}
|
||||
let size: NumberSize = NumberUtils.roundNumber(value, level, decimal);
|
||||
return this.decimalPipe.transform(size.number) + (size.size?'<span class="number-size">' + size.size + '</span>':'');
|
||||
return this.decimalPipe.transform(size.number) + (size.size ? '<span class="number-size">' + size.size + '</span>' : '');
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue