Angular 2 formatting currency BRL format

前端 未结 11 2416
梦毁少年i
梦毁少年i 2021-02-19 14:52

I\'m trying to use pipe to format the price of an item in PT-BR currency format.

Here what i\'m trying to do:

{{statement
11条回答
  •  被撕碎了的回忆
    2021-02-19 15:40

    I solved with an workaround that adapts based on country:

    import { Pipe, PipeTransform, LOCALE_ID, Inject } from '@angular/core';
    import { getLocaleCurrencySymbol, getLocaleCurrencyName } from 
    '@angular/common';
    
    @pipe({
    name: 'currencyGlobal'
    })
    export class CurrencyGlobalPipe implements PipeTransform {
      constructor(@Inject(LOCALE_ID) public locale: string){
      }
    
      transform(value: number): any {
        return getLocaleCurrencySymbol(this.locale) + new 
         Intl.NumberFormat(this.locale, { style: 'decimal', minimumFractionDigits: 2 
       }).format(value);
    }
    
    }
    

    Couldn't use the Intl style:'currency' because the getLocaleCurrencyName doens't return the same as the documentation says it does (https://angular.io/api/common/getLocaleCurrencyName), should be 'USD' but return 'US Dollar', so I did an workaround with currencySimbol + decimal.

    Maybe it can help someone else

提交回复
热议问题