Yogesh Chauhan's Blog

CurrencyPipe in Angular 9 with Examples

in Angular on September 12, 2020

What is CurrencyPipe?

📌 It’s a built-in pipe from Angular that transforms a number to a currency string.

The CurrencyPipe formats according to locale rules that determine group sizing and separator, decimal-point character, and other locale-specific configurations.

Syntax


{{ value | currency [ : currencyCode [ : display [ : digitsInfo [ : locale ] ] ] ] }}

Where

value (any)

The number to be formatted as currency (any type)

currencyCode (string) 

The ISO 4217 currency code, such as USD for the US dollar and EUR for the euro.

NOTE: The default currency code can be configured using the DEFAULT_CURRENCY_CODE injection token.

It is optional. Default is undefined.

display (string | boolean)

The format for the currency indicator.

One of the following:

code: Show the code (such as USD).

symbol(default): Show the symbol (such as $).

symbol-narrow: Use the narrow symbol for locales that have two symbols for their currency.

For example, the Canadian dollar CAD has the symbol CA$ and the symbol-narrow $.

If the locale has no narrow symbol, uses the standard symbol for the locale.

String: Use the given string value instead of a code or a symbol. For example, an empty string will suppress the currency & symbol.

Boolean (marked deprecated in v5): true for symbol and false for code.

Optional. Default is ‘symbol‘.

digitsInfo (string)

Decimal representation options, specified by a string in the following format:

{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}.

minIntegerDigits: The minimum number of integer digits before the decimal point.

Default is 1.

minFractionDigits: The minimum number of digits after the decimal point.

Default is 2.

maxFractionDigits: The maximum number of digits after the decimal point.

Default is 2.

If not provided, the number will be formatted with the proper amount of digits, depending on what the ISO 4217 specifies.

For example, the Canadian dollar has 2 digits, whereas the Chilean peso has none.

Optional. Default is undefined.

locale (string)

A locale code for the locale format rules to use.

When not supplied, uses the value of LOCALE_ID, which is en-US by default. 

Optional. Default is undefined.

📌 The default currency code is currently always USD but this is deprecated from v9.

In v11 the default currency code will be taken from the current locale identified by the LOCAL_ID token.

If you need the previous behavior then set it by creating a DEFAULT_CURRENCY_CODE provider in your application NgModule:


{provide: DEFAULT_CURRENCY_CODE, useValue: 'USD'}

Examples


@Component({
  selector: 'currency-pipe',
  template: `<div>
    <!--output '$0.26'-->
    <p>A: {{a | currency}}</p>

    <!--output 'CA$0.26'-->
    <p>A: {{a | currency:'CAD'}}</p>

    <!--output 'CAD0.26'-->
    <p>A: {{a | currency:'CAD':'code'}}</p>

    <!--output 'CA$0,001.35'-->
    <p>B: {{b | currency:'CAD':'symbol':'4.2-2'}}</p>

    <!--output '$0,001.35'-->
    <p>B: {{b | currency:'CAD':'symbol-narrow':'4.2-2'}}</p>

    <!--output '0 001,35 CA$'-->
    <p>B: {{b | currency:'CAD':'symbol':'4.2-2':'fr'}}</p>

    <!--output 'CLP1' because CLP has no cents-->
    <p>B: {{b | currency:'CLP'}}</p>
  </div>`
})
export class CurrencyPipeComponent {
  a: number = 0.259;
  b: number = 1.3495;
}

Credit: Angular.io


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 PHP Login System using PDO Part 1: Create User Registration Page #6 How to uninstall Cocoapods from the Mac OS?

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How JavaScript classes allows us to do less typing (syntactic sugar)?JavaScriptTackle Accessibility in React with JSXReactHow to define visibility for a property in PHP?PHPKilling A Project Part 1: What criteria should be used to cancel/kill a project?MiscellaneousWhat are Identifiers in JavaScript?JavaScriptSteps to Secure a VPNMiscellaneous