You've already forked openaccounting-web
forked from cybercinch/openaccounting-web
initial commit
This commit is contained in:
27
src/app/shared/currency-format.pipe.ts
Normal file
27
src/app/shared/currency-format.pipe.ts
Normal file
@@ -0,0 +1,27 @@
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { DecimalPipe } from '@angular/common';
|
||||
|
||||
@Pipe({name: 'currencyFormat'})
|
||||
export class CurrencyFormatPipe implements PipeTransform {
|
||||
constructor(private decimalPipe: DecimalPipe) {
|
||||
}
|
||||
|
||||
transform(amount: number, precision: number, currency = 'USD'): string {
|
||||
if(amount === null || amount === undefined) {
|
||||
return '';
|
||||
}
|
||||
|
||||
let prefix = amount < 0 ? '-' : '';
|
||||
|
||||
if(currency === 'USD') {
|
||||
prefix += '$';
|
||||
}
|
||||
|
||||
let minDigits = Math.min(2, precision);
|
||||
|
||||
return prefix +
|
||||
this.decimalPipe.transform(
|
||||
Math.abs(amount) / Math.pow(10, precision),
|
||||
'1.' + minDigits + '-' + precision);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user