here's an example of how to use the CurrencyPipe
in Angular:
- First, import the
CurrencyPipe
from the@angular/common
module:
import { CurrencyPipe } from '@angular/common';
- Inject the
CurrencyPipe
into your component's constructor:
constructor(private currencyPipe: CurrencyPipe) { }
- Use the
transform()
method of theCurrencyPipe
to format a number as currency in your component's template:
<p>The price is {{ price | currency:'USD':'symbol':'1.2-2' }}</p>
In this example, price
is a number variable that represents the price of a product. The currency
pipe is used to format this number as US dollars with the dollar symbol, 2 decimal places, and a minimum of 1 integer digit. The resulting output will look something like this:
The price is $9.99
You can customize the formatting of the currency by specifying different options as the arguments to the currency
pipe. The first argument specifies the ISO currency code (e.g. 'USD'
, 'EUR'
, 'GBP'
, etc.), the second argument specifies the display format for the currency symbol (e.g. 'symbol'
, 'code'
, 'name'
, or false
to suppress the symbol), and the third argument specifies the number format options (e.g. '1.0-0'
, '1.2-2'
, etc.). The CurrencyPipe
also supports options for specifying the decimal separator, thousands separator, and locale, which can be useful for internationalization purposes.
Comments
Post a Comment