• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • Angular

Pipes in Angular Part – 3 : CurrencyPipe, PercentPipe

  • Yatendrasinh Joddha
  • July 3, 2018
  • 3 minute read
Angular
Total
0
Shares
0
0
0

Here is the third installment of angular pipe series in this part we will discuss about CurrencyPipe, and PercentPipe, If you have not read previous articles here is the link for previous articles.

  • Pipes in Angular Part – 1 : lowercase, uppercase, and titlecase pipes
  • Pipes in Angular Part – 2 : DatePipe

CurrencyPipe, and PercentPipe, Both are used to transform a number to a specific type of currency, and percentage value.

CurrencyPipe

CurrencyPipe is used to transform a number to a currency string according to the locale rule.

Syntax

{{ value_expression | currency [ : currencyCode [ : display [ : digitsInfo [ : locale ] ] ] ] }}
Consider the below code to apply currencyPipe and which we will use in our demos.
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  amount: number = 20120;
}

app.component.html

<div style="text-align:left">
  <p>Amount is : {{amount | currency}}
</div>

//Output: Amount is : $20,120.00

This is the way by which we can apply currencypipe. Now let us discuss and apply all the parameters to format currency in more ways.

currencyCode

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

  • The currencyCode such as INR for Indian Rupee , USD for US dollar, EUR for the euro
  • Default it is undefined
  • Check this out for full list of currency code
<div style="text-align:left">
  <p>Amount is : {{amount | currency : 'INR'}}
</div>

//Output: Amount is : ₹20,120.00

display

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

This is used to display the format of the currency indicator. It has certain values listed in below table. But before that let us see how to use one of them

<div style="text-align:left">
  <p>Amount is : {{amount | currency : 'INR' : 'code'}}
</div>

//Output: Amount is : INR20,120.00

Here, in output we can see ‘INR’ instead of rupee symbol.  So here is the full table for display values.

[table style=”3″] [tr][th]value[/th] [th]Description[/th] [th]Output[/th][/tr] [tr][td]code[/td] [td]It will show the code like INR[/td] [td]Amount is : INR20,120.00[/td][/tr] [tr][td]symbol[/td] [td]This is default, and it will show symbol like ₹[/td] [td]Amount is : ₹20,120.00 [/td][/tr] [tr][td]symbol-narrow[/td] [td]Use this 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[/td][td]Amount is : {{amount | currency : 'CAD' : 'symbol-narrow'}}

Amount is : $20,120.00[/td][/tr] [tr][td]string[/td] [td]This is used to display a given string instead of any code or symbol[/td] [td]Amount is : {{amount | currency : 'hello'}} //Output:Amount is : hello20,120.00[/td][/tr]

[/table]

digitsInfo

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

Decimal representation options, specified by a string in the following format: {minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}

Let us consider a below code to understand uses. For that we need to change value of amount.
amount: number = 0.12;

CurrencyPipe

  • 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 0.
  • maxFractionDigits: The maximum number of digits after the decimal point. Default is 3.

locale

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

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.

PercentPipe

It is used to transform a number to a percentage string according to a locale rules.

Syntax

{{ value_expression | percent [ : digitsInfo [ : locale ] ] }}
Consider the below code for the usage
app.component.ts
import { Component } from '@angular/core';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  num1: number = 0.12;
}

app.component.html

<div style="text-align:left">
  <p>Percentage is : {{num1 | percent}}
</div>

//Output: Percentage is : 12%

There are two parameters which we can pass with the percentPipe,

1. digitsInfo

2. locale.

This both parameter works same as currencyPipe. Consider that description for the same, and let us see a small example by passing digitsInfo parameter in above example and see the output

<div style="text-align:left">
  <p>Percentage is : {{num1 | percent : '2.2-2'}}
</div>

//Output: Percentage is : 12.00%

Summary

In this article we have discussed how to use currency, and percent pipe to format a number. In next article we will have a look on SlicePipe, JSON Pipe and Async Pipe.

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 11,874

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    1 year ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    2 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
  • New Learning Models for Fall 2020

    New Learning Models for Fall 2020

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

 

Loading Comments...
 

    %d