What is the CurrencyPipe?
The CurrencyPipe is a built-in Angular pipe that's designed to format numbers as currency values according to specified locales, currency codes, and display rules. It's a convenient tool for ensuring that monetary values are presented in a way that's familiar and readable for users in different regions of the world.
Basic Usage
The most straightforward way to use the CurrencyPipe is like this in your Angular template:
<p>Product Price: {{ price | currency }}</p>
price
: The numerical value you want to format as currency.currency
The pipe's name.
If you don't provide further options, it will use your browser's default locale and currency.
Customization Options
The CurrencyPipe offers several customization options:
Currency Code (
USD
,EUR
, etc.)HTML<p>Product Price: {{ price | currency:'GBP' }}</p> ```
Display Format
'code'
: Display the currency code (e.g., USD, EUR).'symbol'
(default): Display the currency symbol (e.g., $, €).'symbol-narrow'
: Display a narrow version of the currency symbol when available.- Custom string: Provide your own string for formatting.
HTML<p>Product Price: {{ price | currency:'USD':'symbol-narrow' }}</p>
Decimal Digits You can control the number of digits after the decimal point using a colon-separated format:
HTML<p>Product Price: {{ price | currency:'USD':'symbol':'1.2-2' }}</p>
Example
Let's assume the following in your Angular component:
price = 1234.56;
Here are examples of the output with different CurrencyPipe configurations:
0 Comments