← Learning Hub
Intermediate

Pipes: Formatting Values

A pipe formats a value in the template with the | sign. It changes only how the value looks, not the value itself. Import the pipes you use into the component.

Pipes keep formatting out of your component logic. The same price might be shown as "₹1,299.00" on one page and "₹1.3K" on another; the stored value stays a plain number, and the template decides how it looks. Angular's built-in pipes handle dates, numbers, currencies, percentages and text case, and they respect the user's language settings.

Example
import { Component } from '@angular/core';
import { CurrencyPipe, DatePipe, UpperCasePipe, DecimalPipe, PercentPipe, SlicePipe } from '@angular/common';

@Component({
  selector: 'app-order',
  imports: [CurrencyPipe, DatePipe, UpperCasePipe, DecimalPipe, PercentPipe, SlicePipe],
  template: `
    <p>{{ name | uppercase }}</p>                       <!-- WIRELESS MOUSE -->
    <p>{{ price | currency: 'INR' }}</p>                 <!-- ₹1,499.00 -->
    <p>{{ price | currency: 'INR' : 'symbol' : '1.0-0' }}</p>   <!-- ₹1,499 -->
    <p>{{ rating | number: '1.1-1' }}</p>                <!-- 4.3 -->
    <p>{{ discount | percent }}</p>                      <!-- 25% -->
    <p>{{ ordered | date: 'd MMM y' }}</p>               <!-- 26 Nov 2026 -->
    <p>{{ description | slice: 0 : 20 }}...</p>
  `,
})
export class Order {
  name = 'Wireless mouse';
  price = 1499;
  rating = 4.25;
  discount = 0.25;
  ordered = new Date(2026, 10, 26);
  description = 'A quiet wireless mouse with a long battery life.';
}

Your own pipe

Example
import { Pipe, PipeTransform } from '@angular/core';

@Pipe({ name: 'timeAgo' })
export class TimeAgoPipe implements PipeTransform {
  transform(date: Date): string {
    const minutes = Math.floor((Date.now() - date.getTime()) / 60000);
    if (minutes < 1) return 'just now';
    if (minutes < 60) return `${minutes} min ago`;
    const hours = Math.floor(minutes / 60);
    return hours < 24 ? `${hours} h ago` : `${Math.floor(hours / 24)} days ago`;
  }
}

// In a template:  {{ review.date | timeAgo }}
Pipes are pure by default: Angular runs them again only when the input value changes. That makes them fast. Keep them simple and free of side effects.
Key points
  • A pipe formats a value with |, e.g. {{ price | currency:'INR' }}.
  • Built-ins: date, currency, number, percent, uppercase.
  • Write your own with @Pipe and transform().

📝 Test yourself

1. Which character applies a pipe in a template?
2. How do you pass an argument to a pipe?
3. Fill in the decorator that creates your own pipe:
@({ name: 'timeAgo' })