Sub topic 6
Formatting numbers, percentages and currency
This chapter assumes you know basic TypeScript, Angular components with template interpolation, and standalone components with an imports array. You should also know how to pass parameters to a pipe after a colon.
Angular provides three pipes for numeric display: number, percent and currency. They live in @angular/common. The built-in pipes are standalone, so you import each one in the component that uses it. The components keep raw numbers, and the template decides how to show them.
The digits-info string
All three pipes share one formatting parameter, called digitsInfo. It is a string with this shape:
{minIntegerDigits}.{minFractionDigits}-{maxFractionDigits}
minIntegerDigitsis the minimum number of digits before the decimal point. Smaller numbers are padded with leading zeros. The default is 1.minFractionDigitsis the minimum number of digits after the point. Missing digits are filled with zeros.maxFractionDigitsis the maximum number of digits after the point. Extra digits are rounded away.
So '1.2-2' always shows exactly two decimals, and '3.0-0' shows at least three integer digits and no decimals.
The number pipe
The number pipe formats a plain number with grouping separators. Without a parameter it shows at least one integer digit and between zero and three decimals.
import { Component } from '@angular/core';
import { DecimalPipe } from '@angular/common';
@Component({
selector: 'app-stats',
standalone: true,
imports: [DecimalPipe],
template: `
<p>{{ visitors | number }}</p>
<p>{{ average | number:'1.1-2' }}</p>
<p>{{ rank | number:'3.0-0' }}</p>
`,
})
export class StatsComponent {
visitors = 1234567.891;
average = 4.5;
rank = 7;
}In the en-US locale this renders 1,234,567.891, 4.50 and 007. The class is named DecimalPipe, while the name used in templates is number.
The percent pipe
The percent pipe multiplies the value by 100 and appends a percent sign. A ratio of 0.256 is shown as a percentage, not a value of 0.256%. By default it shows between zero and zero fraction digits, so the result is rounded to a whole percent.
<p>{{ 0.256 | percent }}</p>
<p>{{ 0.256 | percent:'1.0-1' }}</p>
<p>{{ 1.5 | percent }}</p>These render 26%, 25.6% and 150%. A common mistake is passing 25 and expecting 25%. You would get 2,500%, so pass 0.25 instead.
The currency pipe
The currency pipe takes up to four parameters, in this order:
- The ISO 4217 currency code, such as
'USD'or'EUR'. - The display style:
'symbol'(the default),'narrow'for a shorter symbol,'code'to show the code, or a custom string. - The digitsInfo string.
- An optional locale that overrides the application locale.
<p>{{ price | currency }}</p>
<p>{{ price | currency:'EUR' }}</p>
<p>{{ price | currency:'EUR':'code' }}</p>
<p>{{ price | currency:'EUR':'symbol':'1.0-0' }}</p>With price = 1234.5 in en-US, these render $1,234.50, €1,234.50, EUR1,234.50 and €1,235. The default currency code is USD, which is easy to miss. If your data is in another currency, always pass the code. You can also set a different default for the whole application by providing DEFAULT_CURRENCY_CODE.
The currency decides the default number of decimals. For example, the Japanese yen has none, so {{ 1500 | currency:'JPY' }} renders ¥1,500.
flowchart TDHow the pipes handle odd input
All three pipes return null for null, undefined or an empty string, so the template shows nothing. They throw an error for a string that is not numeric, such as 'abc'. A numeric string like '42.5' is accepted and converted.
Locale effects
The separators and symbol position depend on the active locale. In en-US, 1234.5 becomes 1,234.5. In German it becomes 1.234,5. The next chapters cover how to set the locale. For one-off cases, the currency pipe accepts a locale as its fourth parameter, while number and percent accept it as their second.
Exercise
A component has rate = 0.0725 and cost = 49. Write template expressions that show the rate as 7.3% and the cost as euros with no decimals, as €49.
Show answer
{{ rate | percent:'1.0-1' }} and {{ cost | currency:'EUR':'symbol':'1.0-0' }}. The percent pipe multiplies 0.0725 by 100 to get 7.25 and rounds to one decimal digit, giving 7.3%. The currency digitsInfo '1.0-0' removes the decimals.