In this article, we are going to see what is percentPipe in Angular 10 and how to use it.
The percentPipeĀ is used to Transform a number to a percentage string.
Syntax:
{{ value | percent [ : digitsInfo [ : locale ] ] }}
NgModule: Module used by percentPipe is:
- CommonModule
Approach:
- Create the angular app to be used
- There is no need for any import for the percentPipeĀ to be used
- In app.component.ts define the variables that take the percentPipeĀ value.
- In app.component.html use the above syntax with '|' symbol to make percentPipeĀ element.
- Serve the angular app using ng serve to see the output
Input value:
- value: it takes a string value.
Parameters:
- digitsInfo: it takes a string value.
- locale: it takes a string value.
Example 1:
Filename: app.component.tsimport { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
geeks: number = 0.4945;
gfg: number = 2.343564;
}
Ā Ā
Filename: app.component.html<div>
<p>1st: {{gfg | percent}}</p>
<p>2nd: {{geeks | percent:'4.3-5'}}</p>
</div>
Ā Ā
Output:
Ā
Ā
Example 2:
Ā
import { Component, OnInit } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
geeks: number = 100;
}
Ā Ā
Filename: app.component.html<div>
<p>1st: {{geeks | percent }}</p>
<p>2nd: {{geeks | percent:'3.4-5' }}</p>
</div>
Ā Ā
Output:
Ā
Ā
Reference: https://v17.angular.io/api/common/PercentPipe
Ā