Angular 2格式化货币BRL格式

Cla*_*ous 11 currency angular

我正在尝试用管道来格式化PT-BR货币格式的商品价格.

这是我想要做的:

<div class="desc">{{statement.price | currency:'BRL':true:'1.2-2'}} </div>  
Run Code Online (Sandbox Code Playgroud)

我期望的结果是33.111,00,现在返回33,111.00.

小智 16

您可以设置locale-id.导入模块如下:

import {LOCALE_ID} from '@angular/core';
Run Code Online (Sandbox Code Playgroud)

在您的模块中定义一个这样的提供者:

providers: [
    {
      provide: LOCALE_ID,
      useValue: "en-US"
    }
]
Run Code Online (Sandbox Code Playgroud)

只需交换您的区域设置ID(对于ID,请参阅Angular文档).


s_b*_*ead 10

我试图使用这个管道将我的号码格式化为巴西货币

{{p.preco | currency : 'R$' }}
Run Code Online (Sandbox Code Playgroud)

我必须在 app.module.ts 中添加以下几行才能正确格式化货币(即 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'
        },
    ]
})
Run Code Online (Sandbox Code Playgroud)

直到我添加registerLocaleData电话才成功

即使您不提供货币符号 (R$),它也能按预期工作,您应该只调用管道,angular 将查找DEFAULT_CURRENCY_CODE上面声明的内容:

{{p.preco | currency }}
Run Code Online (Sandbox Code Playgroud)

我正在使用 angular 10,希望它有所帮助!


Cla*_*sos 5

我解决....

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;
  }

}
Run Code Online (Sandbox Code Playgroud)


Fáb*_*nin 5

对我来说,它在导入语言环境后就可以正常工作了,正如Marcelo Vieira das Neves所提到的那样。

  1. 在模块中导入语言环境:

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

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

  1. 使用货币管道

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


Nil*_*z11 1

您使用的是哪种浏览器,如代码中所述:

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/].
Run Code Online (Sandbox Code Playgroud)

也许你最好使用另一个库,就像他们提到的那样。