Built-in Pipes
Learn the commonly used built-in Angular Pipes for formatting strings, numbers, dates, currencies, and other values.
What are Built-in Pipes?
Angular provides several built-in pipes that can be used directly inside templates to transform and format data.
These pipes help display component data in a clean and user-friendly format without modifying the original value.
Built-in Pipe Flow
Original Value
Component contains the original data.
Pipe
A built-in pipe transforms the value.
Template
The transformed value is used in the template.
Output
The formatted value is displayed to the user.
Common Built-in Pipes
UpperCase
Converts text into uppercase characters.
uppercase
LowerCase
Converts text into lowercase characters.
lowercase
Date
Formats date values.
date
Currency
Formats numbers as currency.
currency
1. Uppercase Pipe
The uppercase pipe converts all characters in a
string to uppercase.
studentName = 'Samiksha';
<p>
{{studentName | uppercase}}
</p>
2. Lowercase Pipe
The lowercase pipe converts all characters in
a string to lowercase.
<p>
{{studentName | lowercase}}
</p>
3. Date Pipe
The date pipe formats a date value into a
readable format.
joiningDate = new Date();
<p>
{{joiningDate | date}}
</p>
Date with Format
The date pipe can accept a format parameter.
<p>
{{joiningDate | date:'dd/MM/yyyy'}}
</p>
4. Currency Pipe
The currency pipe formats numeric values as
currency.
courseFee = 35000;
<p>
{{courseFee | currency:'INR'}}
</p>
5. Number Pipe
The number pipe formats numeric values according
to the specified digit information.
percentage = 87.5678;
<p>
{{percentage | number:'1.2-2'}}
</p>
6. Percent Pipe
The percent pipe formats a number as a percentage.
completion = 0.85;
<p>
{{completion | percent}}
</p>
7. JSON Pipe
The json pipe is useful for displaying objects
and arrays in JSON format, especially while debugging.
student = {
name: 'Sam',
course: 'Angular',
fee: 35000
};
<pre>
{{student | json}}
</pre>
8. Slice Pipe
The slice pipe creates a subset of an array
or string for display.
students = [
'Sam',
'Pradnya',
'Samiksha',
'Rahul'
];
<p>
{{students | slice:0:2}}
</p>
9. Async Pipe
The async pipe subscribes to an Observable or
Promise and displays the emitted value.
<p>
{{studentData$ | async}}
</p>
Built-in Pipe Reference
| Pipe | Purpose | Example |
|---|---|---|
uppercase |
Convert text to uppercase | {{name | uppercase}} |
lowercase |
Convert text to lowercase | {{name | lowercase}} |
date |
Format dates | {{date | date}} |
currency |
Format currency | {{fee | currency:'INR'}} |
number |
Format numbers | {{value | number}} |
percent |
Format percentage | {{value | percent}} |
json |
Display objects as JSON | {{student | json}} |
slice |
Extract part of a value | {{students | slice:0:2}} |
async |
Handle Observable or Promise values | {{data$ | async}} |
Complete Student Example
export class StudentComponent {
studentName = 'Samiksha';
courseFee = 35000;
completion = 0.85;
joiningDate = new Date();
student = {
name: 'Sam',
course: 'Angular'
};
}
<h3>
{{studentName | uppercase}}
</h3>
<p>
Course Fee:
{{courseFee | currency:'INR'}}
</p>
<p>
Completion:
{{completion | percent}}
</p>
<p>
Joining Date:
{{joiningDate | date:'dd/MM/yyyy'}}
</p>
<pre>
{{student | json}}
</pre>
SAMIKSHA
Course Fee: ₹35,000.00
Completion: 85%
Angular Student
Best Practices
- Use built-in pipes whenever they satisfy the requirement.
- Use pipes mainly for presentation and formatting.
- Keep complex business logic outside the template.
- Use parameters when customized formatting is required.
- Use the JSON pipe mainly for debugging purposes.
- Avoid unnecessary pipe chains in complex templates.
Summary
- Angular provides many built-in pipes.
uppercaseandlowercaseformat text.dateformats date values.currencyformats monetary values.numberformats numeric values.percentformats percentages.jsonhelps inspect objects and arrays.sliceextracts a portion of a value.asynchandles Observable and Promise values.