Angular percent Pipe
April 08, 2024
On this page we will learn to use percent
pipe in our Angular application. Percent Pipe formats a number as percentage. It uses percent keyword. We can format a number with percent
pipe with default format as well as custom format.
1. PercentPipe
AngularPercentPipe
is an angular Pipe
API that formats a number as a percentage according to digit options and locale rules. It belongs to CommonModule
.
Find its syntax.
{{ number_expression | percent [ : digitsInfo [ : locale ] ] }}
a. number_expression : A number or an expression that will give output as number to be formatted.
b. percent : A pipe keyword that is used with pipe operator and it converts number into percent.
c. digitInfo : It defines a percentage format.cIt is used with following syntax.
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minFractionDigits : Minimum number of fraction digits after decimal point. Default is 0.
maxFractionDigits : Maximum number of fraction digits after decimal point. Default is 0.
d. locale : Specify locale that allows to use locale specific format rules. By default, locale is accessed by LOCALE_ID which is en-US by default. LOCALE_ID is used to set the locale of the application.
2. Using percent Pipe
A. Using default format :minIntegerDigits = 1
minFractionDigits = 0
maxFractionDigits = 0
1. Find a number that will be changed into percentage.
TS code:
num1 = 2.5;
HTML code:
<p> {{num1 | percent}} </p>
2.
{{0.05 | percent}}
3.
{{0.5 | percent}}
4.
{{1 | percent}}
5.
{{5.5 | percent}}
6.
{{5.05 | percent}}
7.
{{5 | percent}}
B. Custom format with digitsInfo
1. Use format '2.2-5'
minIntegerDigits = 2
minFractionDigits = 2
maxFractionDigits = 5
Find the number that will be changed into percentage.
num1 = 2.5;
{{num1 | percent:'2.2-5'}}
250.00%
Find another number with percent pipe.
{{2.523523523 | percent:'2.2-5'}}
We can see that maximum fraction digit is 5.
2.
{{0.05 | percent:'1.1-3'}}
3.
{{0.05 | percent:'2.1-3'}}
4.
{{0.05 | percent:'2.2-3'}}
C. Custom format with digitsInfo and locale
1.
{{ 10.125 | percent: '2.1-3' : 'en_CA' }}
2.
{{ 0.5234 | percent: '2.1-3' : 'en_US' }}
3.
{{ 1.345 | percent: '2.1-3' : 'en_GB' }}
3. Using PercentPipe in Component
To format number usingPercentPipe
in component, we can use its transform()
method.
Find its syntax.
transform(value: number | string, digitsInfo?: string, locale?: string)
Instantiate
PercentPipe
with default locale.
const percent = new PercentPipe("en_US");
transform()
method.
const n = 0.03; const out1 = percent.transform(n); console.log(out1); // 3% const out2 = percent.transform(n, '1.2-3'); console.log(out2); // 3.00% const out3 = percent.transform(n, '2.2-3', "en_GB"); console.log(out3); // 03.00%
4. Complete Example
percent.component.html<h3>Percent Pipe</h3> <div> <p> {{num1 | percent}} </p> <p> {{num1 | percent:'2.2-5'}} </p> <p> {{num2 | percent:'1.2-5' : 'en_CA'}} </p> <p> {{num1 * num2 | percent:'1.2-3' : 'en_GB'}} </p> </div>
import { Component, OnInit } from '@angular/core'; import { CommonModule, PercentPipe, TitleCasePipe } from '@angular/common'; @Component({ selector: 'app-percent', standalone: true, imports: [CommonModule], templateUrl: './percent.component.html' }) export class PercentComponent implements OnInit { num1: number = 2.5; num2: number = 0.5; ngOnInit(): void { this.convertInTitleCase(); } convertInTitleCase() { const percent = new PercentPipe("en_US"); const n = 10.1125; const out1 = percent.transform(n); console.log(out1); // 1,011% const out2 = percent.transform(n, '2.2-5'); console.log(out2); // 1,011.25% const out3 = percent.transform(n, '2.2-5', "en_GB"); console.log(out3); // 1,011.25% } }