currency-format.pipe.ts
angular/comprehensive-demo/src/app/shared/pipes/currency-format.pipe.ts
/**
* Currency Format Pipe
*
* Custom pipe for formatting currency values.
* This is a simple example - Angular's built-in CurrencyPipe is more feature-rich.
*
* This demonstrates:
* - Custom formatting logic
* - Parameter handling
* - Default values
*/
import { Pipe, PipeTransform } from '@angular/core';
@Pipe({
name: 'currencyFormat',
standalone: true
})
export class CurrencyFormatPipe implements PipeTransform {
/**
* Format a number as currency
*
* @param value - Numeric value to format
* @param currency - Currency code (default: 'USD')
* @param symbol - Currency symbol (default: '$')
* @returns Formatted currency string
*
* @example
* 1234.56 | currencyFormat -> '$1,234.56'
* 1234.56 | currencyFormat:'EUR':'€' -> '€1,234.56'
*/
transform(value: number | null | undefined, currency: string = 'USD', symbol: string = '$'): string {
if (value === null || value === undefined || isNaN(value)) {
return `${symbol}0.00`;
}
// Format number with commas and 2 decimal places
const formatted = value.toFixed(2).replace(/\B(?=(\d{3})+(?!\d))/g, ',');
return `${symbol}${formatted}`;
}
}
Articoli correlati
server.js
server.js — javascript source code from the angular learning materials (angular/comprehensive-demo/server/server.js).
Leggi l'articolo →app.component.ts
app.component.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.component.ts).
Leggi l'articolo →app.config.ts
app.config.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.config.ts).
Leggi l'articolo →app.routes.ts
app.routes.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/app.routes.ts).
Leggi l'articolo →auth.guard.ts
auth.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/auth.guard.ts).
Leggi l'articolo →role.guard.ts
role.guard.ts — typescript source code from the angular learning materials (angular/comprehensive-demo/src/app/core/guards/role.guard.ts).
Leggi l'articolo →