diff --git a/src/app/shared/currency-format.pipe.ts b/src/app/shared/currency-format.pipe.ts index 345b379..edbd758 100644 --- a/src/app/shared/currency-format.pipe.ts +++ b/src/app/shared/currency-format.pipe.ts @@ -1,7 +1,16 @@ import { Pipe, PipeTransform } from '@angular/core'; import { DecimalPipe } from '@angular/common'; -@Pipe({name: 'currencyFormat'}) +// Format the currency according to the user's browser locale. +// +// Some currencies share the same symbol (e.g. $ is used for USD and CAN). +// Ideally, the user experience should not be confusing if different accounts +// have conflicting symbols. One solution might be to always show the ISO +// currency code for accounts with mixes currencies. Another solution would be +// to let the user configure how the currency is displayed on a per-currency +// or per-account basis. + +@Pipe({ name: 'currencyFormat' }) export class CurrencyFormatPipe implements PipeTransform { constructor(private decimalPipe: DecimalPipe) { } @@ -11,17 +20,12 @@ export class CurrencyFormatPipe implements PipeTransform { 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); + // note: we can drop the cast to any if we change the ts target from es5 to es2020 or newer. + return Intl.NumberFormat(navigator.language, { + style: "currency", + currency, + minimumFractionDigits: precision, + signDisplay: "negative", + } as any).format(amount / Math.pow(10, precision)); } } \ No newline at end of file