Angular 2 formatting currency BRL format

前端 未结 11 2466
梦毁少年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:31

    i solved this way:

    import { Pipe, PipeTransform } from '@angular/core';
    import { CurrencyPipe } from '@angular/common';
    
    @Pipe({
      name: 'currencyFormat'
    })
    export class CurrencyFormatPipe implements PipeTransform {
        transform(value: number, locale: string, currency_symbol: boolean, number_format: string = '1.2-2'): string {
            if (value) {
    
                let currencyPipe = new CurrencyPipe();
                let new_value: string;
    
                new_value = currencyPipe.transform(value, locale, currency_symbol, number_format);
                if (locale = 'BRL') {
                    new_value = new_value.replace('.', '|').replace(',', '.').replace('|', ',');
                } 
    
                return new_value                                    
            }
        }
    }
    

提交回复
热议问题