Angular 2 formatting currency BRL format
Asked Answered
M

13

18

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:

<div class="desc">{{statement.price | currency:'BRL':true:'1.2-2'}} </div>  

The result that i expect is 33.111,00 and now is returning 33,111.00 .

Mislike answered 3/8, 2016 at 19:41 Comment(0)
S
19

You can set the locale-id. Import the module as follows:

import {LOCALE_ID} from '@angular/core';

And in your module define a provider like this:

providers: [
    {
      provide: LOCALE_ID,
      useValue: "en-US"
    }
]

Just exchange for your locale ID (for IDs, refer to the Angular documentation).

Sacring answered 27/2, 2017 at 6:56 Comment(2)
If you need to do this app wide, this should be the accepted answer.Mcmillan
This is the correct answer. But the 'p' in provide: should be lower case. { provide: LOCALE_IDBreeches
S
18

I was trying to format my number to Brazilian currency using this pipe

{{p.preco | currency : 'R$' }}

and I had to put the following lines in app.module.ts in order to format the currency correctly (i.e. R$ 12,00)

import {LOCALE_ID, DEFAULT_CURRENCY_CODE} from '@angular/core';
import localePt from '@angular/common/locales/pt';
import {registerLocaleData} from '@angular/common';

registerLocaleData(localePt, 'pt');

@NgModule({
    declarations: [/*...*/],
    imports: [/*...*/],
    providers: [
        {
            provide: LOCALE_ID,
            useValue: 'pt'
        },

        /* if you don't provide the currency symbol in the pipe, 
        this is going to be the default symbol (R$) ... */
        {
            provide:  DEFAULT_CURRENCY_CODE,
            useValue: 'BRL'
        },
    ]
})

It didn't work out until I add the registerLocaleData call

It also works as expected even if you don't provide the currency symbol (R$), you should just call the pipe and angular will look for the DEFAULT_CURRENCY_CODE declared above:

{{p.preco | currency }}

I'm using angular 10, hope it helps!

Sika answered 5/12, 2019 at 23:31 Comment(3)
Where did DEFAULT_CURRENCY_CODE come from?Profundity
@BrunoSantos import {DEFAULT_CURRENCY_CODE} from '@angular/core' I'm gonna update the answerSika
yours was the only answer that solved for me more than a year in the future, thanksPolitick
H
11

For me it worked after importing the locale, as mentioned by Marcelo Vieira das Neves.

  1. Import locale in your module:

import { LOCALE_ID } from '@angular/core';

providers: [{provide: LOCALE_ID, useValue: 'pt-BR'}]

  1. Use the currency pipe

{{item.limite | currency:'BRL':true}}

Hialeah answered 9/8, 2017 at 13:54 Comment(0)
L
6

I solve....

import { Pipe, PipeTransform } from '@angular/core';
import { CurrencyPipe } from '@angular/common';

@Pipe({
  name: 'currencyformat'
})
export class CurrencyFormatPipe implements PipeTransform {

  transform(value: number, currencyCode: string = 'BRL', symbolDisplay: boolean = true, digits?: string): string {
    if (!value) {
      return '';
    }

    let currencyPipe: CurrencyPipe = new CurrencyPipe('pt-BR');
    let newValue: string = currencyPipe.transform(value, currencyCode, symbolDisplay, digits);

    return newValue;
  }

}
Leathern answered 6/10, 2016 at 21:21 Comment(0)
D
5

I solved this way:

app.module.ts

import { LOCALE_ID } from '@angular/core';
import localePt from '@angular/common/locales/pt';
import {registerLocaleData} from '@angular/common';
registerLocaleData(localePt)

 providers: [{
    provide: LOCALE_ID, 
    useValue: "pt-BR"
  }],

.html

currency:'BRL' 
Dekow answered 10/8, 2020 at 11:32 Comment(0)
B
4
getFormattedPrice(price: number) {
    return new Intl.NumberFormat('pt-BR', { style: 'currency', currency: 'BRL' }).format(price);
}
Bozcaada answered 14/2, 2017 at 11:58 Comment(0)
A
1

Which browser are you using, as stated in the code:

WARNING: this pipe uses the Internationalization API. Therefore it is only reliable in Chrome and Opera browsers. For other browsers please use an polyfill, for example: [https://github.com/andyearnshaw/Intl.js/].

Probably you are best to use another library like the one they mentioned.

Auraaural answered 3/8, 2016 at 19:44 Comment(1)
Thanks, I think that's my problem, I'll find another API to help me with this.Mislike
T
1

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                                    
        }
    }
}
Technology answered 30/9, 2016 at 13:39 Comment(0)
S
1

module

import { LOCALE_ID, DEFAULT_CURRENCY_CODE } from '@angular/core';
import localePt from '@angular/common/locales/pt';
registerLocaleData(localePt);

providers: [{
      provide: LOCALE_ID,
      useValue: "pt-BR"
    },
    {
      provide:  DEFAULT_CURRENCY_CODE,
      useValue: 'BRL'
    },
  ]

html

{{(price | currency)}}
Stempson answered 9/12, 2021 at 13:34 Comment(0)
M
1

At this moment on Angular 13.2.2 you may do like this:

app.module.ts

import { LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localePt from '@angular/common/locales/pt';
registerLocaleData(localePt);

  providers: [
    { provide: LOCALE_ID, useValue: 'pt-BR' }
  ],

component.html

<div>    
   Valor: {{ valor | currency:'BRL' }}
</div>
Myke answered 30/4, 2022 at 23:18 Comment(0)
O
0

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

Octosyllabic answered 11/4, 2018 at 14:48 Comment(0)
B
0

in app.module.ts

`//add support to pt-BR
import { LOCALE_ID } from '@angular/core';
import { registerLocaleData } from '@angular/common';
import localePt from '@angular/common/locales/pt';
registerLocaleData(localePt);
//add support to pt-BR`


`providers: [ {provide: LOCALE_ID, useValue: "pt-BR" }]//add support to pt-BR`

at the page html {{product.price| currency}}

Borghese answered 15/1, 2021 at 20:9 Comment(0)
D
0

Using Angular CLI 14.2.4 you don't need any import on app.module.ts. Just use in your template.html :

{{ product.price | currency:'BRL' }}

And will work.

Donnetta answered 4/10, 2022 at 19:52 Comment(1)
It actually doesn't format the right way, the result is R$ 1,000,000.00, when it should be using dots and not commasTherron

© 2022 - 2024 — McMap. All rights reserved.