2022-12-21 12:37:16 +01:00
|
|
|
import {Pipe, PipeTransform} from "@angular/core";
|
2023-01-11 14:35:13 +01:00
|
|
|
import {Level, NumberSize, NumberUtils} from "../number-utils.class";
|
2022-12-21 12:37:16 +01:00
|
|
|
import {DecimalPipe} from "@angular/common";
|
|
|
|
|
|
|
|
@Pipe({name: 'numberRound'})
|
|
|
|
export class NumberRoundPipe implements PipeTransform {
|
|
|
|
decimalPipe: DecimalPipe = new DecimalPipe("en");
|
2023-04-04 15:48:05 +02:00
|
|
|
|
|
|
|
constructor() {
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
2023-01-11 14:51:55 +01:00
|
|
|
let decimal = 0;
|
2023-04-04 15:48:05 +02:00
|
|
|
if (args[0]) {
|
2023-01-11 14:51:55 +01:00
|
|
|
level = args[0];
|
|
|
|
}
|
2023-04-04 15:48:05 +02:00
|
|
|
if (args[1]) {
|
2023-01-11 14:51:55 +01:00
|
|
|
decimal = args[1];
|
|
|
|
}
|
|
|
|
let size: NumberSize = NumberUtils.roundNumber(value, level, decimal);
|
2023-04-04 15:48:05 +02:00
|
|
|
return this.decimalPipe.transform(size.number) + (size.size ? '<span class="number-size">' + size.size + '</span>' : '');
|
2022-12-21 12:37:16 +01:00
|
|
|
}
|
|
|
|
}
|