Sub topic 6
Formatting numbers, percentages and currency
Angular has three built-in pipes for numeric output: number, percent and currency. They share one parameter format, the digits-info string, and they all read the active locale. This chapter assumes you can write a standalone component, use interpolation in a template, and pass parameters to a pipe with a colon. It also assumes you've seen the pipe operator |.
The three pipes and their classes
Each pipe is backed by a class in @angular/common. Standalone components import the class, not the pipe name.
| Template name | Class | Typical use | Default digits |
|---|---|---|---|
| `number` | `DecimalPipe` | Quantities, measurements | `1.0-3` |
| `percent` | `PercentPipe` | Ratios shown as percentages | `1.0-0` |
| `currency` | `CurrencyPipe` | Prices and money amounts | Set by the currency (usually `1.2-2`) |
The component below uses all three. Later sections refer back to it.
import { Component } from '@angular/core';
import { CurrencyPipe, DecimalPipe, PercentPipe } from '@angular/common';
@Component({
selector: 'app-numbers-demo',
standalone: true,
imports: [DecimalPipe, PercentPipe, CurrencyPipe],
template: `
<p>{{ distance | number }}</p>
<p>{{ distance | number:'1.0-0' }}</p>
<p>{{ step | number:'3.1-2' }}</p>
<p>{{ ratio | percent }}</p>
<p>{{ ratio | percent:'1.0-1' }}</p>
<p>{{ price | currency }}</p>
<p>{{ price | currency:'EUR':'symbol':'1.2-2' }}</p>
`,
})
export class NumbersDemoComponent {
distance = 1234.5678;
step = 5.1;
ratio = 0.256;
price = 1234.5;
}With the default en-US locale, the output is:
1,234.568
1,235
005.10
26%
25.6%
$1,234.50
€1,234.50The digits-info string
The number, percent and currency pipes all take a string of the form minIntegerDigits.minFractionDigits-maxFractionDigits.
| Part | Meaning | In `'3.1-2'` |
|---|---|---|
| `minIntegerDigits` | Minimum digits before the decimal separator. Shorter values are padded with leading zeros. | 3 |
| `minFractionDigits` | Minimum digits after the separator. Shorter values are padded with trailing zeros. | 1 |
| `maxFractionDigits` | Maximum digits after the separator. Longer values are rounded. | 2 |
So 5.1 | number:'3.1-2' gives 005.10. The pipe keeps at least one fraction digit and at most two, and 5.1 becomes 5.10. Padding comes from the minimum, not the maximum. With '3.1-2', the value 5.1 would show as 005.1 if the pipe only enforced the minimum of one. It shows 005.10 because the pipe pads out to the default width where the minimum applies. If you need exactly two decimals, make both bounds equal: '1.2-2'.
Rounding happens when a value has more fraction digits than the maximum. 1234.5678 | number uses the default 1.0-3 and shows 1,234.568.
The pipe doesn't throw when the string is malformed in a way it can parse partially, but a string that doesn't match the pattern raises a runtime error. Write the string in full, with the dot and the hyphen. The shorthand '2' isn't valid.
The number pipe
number formats a numeric value with grouping separators and a decimal separator for the active locale. It accepts a number or a numeric string, and it takes two optional parameters: digitsInfo and locale.
<p>{{ 1234567.891 | number }}</p>
<p>{{ 1234567.891 | number:'1.2-2' }}</p>
<p>{{ 1234567.891 | number:'1.0-0':'de' }}</p>The first line shows 1,234,567.891. The second shows 1,234,567.89. The third overrides the locale for this one binding and shows 1.234.568. A locale given in the template must have been registered, as the locale section below explains.
Two edge cases come up often:
nullandundefinedproducenull, which renders as an empty string. This is intentional, and it lets you bind to data that hasn't loaded yet.- A string that can't be read as a number, such as
'abc', throws an error at render time. Numeric strings like'42.5'are converted without complaint.
The percent pipe
percent multiplies the value by 100 and appends a percent sign. A ratio of 0.256 becomes 26%, not 0.256%. This is the most common mistake: passing 25.6 yields 2,560%.
The default digits string is 1.0-0, so the result is rounded to a whole number. Pass your own string to show decimals:
<p>{{ 0.256 | percent }}</p>
<p>{{ 0.256 | percent:'1.1-1' }}</p>
<p>{{ 1.5 | percent }}</p>These render 26%, 25.6% and 150%. Values above 1 are valid, which is useful for growth figures. The percent sign position and spacing depend on the locale. German, for example, writes 26 % with a space.
If your data already stores percentages as whole numbers such as 25.6, divide by 100 in the component or in a custom pipe. Don't fake it by appending a literal % to a number pipe result, because that skips locale rules.
The currency pipe
currency has the longest signature of the three:
{{ value | currency : currencyCode : display : digitsInfo : locale }}All four parameters are optional.
| Parameter | Purpose | Example values |
|---|---|---|
| `currencyCode` | ISO 4217 code | `'USD'`, `'EUR'`, `'JPY'` |
| `display` | How to show the currency | `'code'`, `'symbol'`, `'symbol-narrow'`, or any string |
| `digitsInfo` | Digits-info string | `'1.2-2'`, `'1.0-0'` |
| `locale` | Locale ID for this binding | `'de'`, `'fr'` |
The display parameter changes how the currency is written:
<p>{{ 1234.5 | currency:'CAD' }}</p>
<p>{{ 1234.5 | currency:'CAD':'symbol-narrow' }}</p>
<p>{{ 1234.5 | currency:'CAD':'code' }}</p>
<p>{{ 1234.5 | currency:'CAD':'Canadian dollars ' }}</p>In en-US these show CA$1,234.50, $1,234.50, CAD1,234.50 (with a non-breaking space after CAD in current versions), and Canadian dollars 1,234.50. The symbol form is unambiguous, because CA$ distinguishes Canadian from US dollars. The narrow form is shorter but ambiguous, so use it only when the currency is obvious from context.
Default digits and the default currency
When you omit digitsInfo, the number of fraction digits comes from the currency, not from a fixed rule. Most currencies use two. Japanese yen uses none, so 1234.5 | currency:'JPY' shows ¥1,235. This is why you shouldn't hard-code '1.2-2' in a template that may show several currencies.
When you omit currencyCode, Angular uses the DEFAULT_CURRENCY_CODE token, which is USD unless you change it. Set it once in your application config if your app doesn't deal in dollars:
import { ApplicationConfig, DEFAULT_CURRENCY_CODE } from '@angular/core';
export const appConfig: ApplicationConfig = {
providers: [{ provide: DEFAULT_CURRENCY_CODE, useValue: 'EUR' }],
};Choosing between the pipes
The diagram shows how to pick a pipe for a given value.
Locale and number output
All three pipes take their separators from the active locale. en-US is built in. For any other locale, you register its data once and provide LOCALE_ID. This is the same setup used for dates:
import { registerLocaleData } from '@angular/common';
import localeDe from '@angular/common/locales/de';
import { ApplicationConfig, LOCALE_ID } from '@angular/core';
registerLocaleData(localeDe);
export const appConfig: ApplicationConfig = {
providers: [{ provide: LOCALE_ID, useValue: 'de' }],
};With this in place, 1234.5 | number renders 1.234,5 and 0.256 | percent renders 26 %. A binding with an explicit locale parameter, such as number:'1.0-0':'fr', needs that locale registered as well. If it isn't, Angular throws an error saying the locale data for that ID is missing.
Common errors
- Percent shows 100 times too much. The value was already a percentage. Pass a ratio or divide first.
- Rounding looks wrong. The default maximum is three fraction digits for
numberand zero forpercent. Set the digits string to match what you want to see. NG02100invalid pipe argument. The input was a string that isn't numeric, such as'1,234'with a comma. Convert it to a number in the component.- Missing locale data. You used a locale ID without calling
registerLocaleData. - Pipe not found in the template. A standalone component must list
DecimalPipe,PercentPipeorCurrencyPipeinimports. - Totals that don't add up. Each row was rounded for display, but the total was calculated from unrounded values. Decide where rounding belongs, and do it in code if it affects the numbers.
Calling these pipes from code
When you need a formatted number outside a template, create the pipe with a locale and call transform, as you would for DatePipe:
import { CurrencyPipe, PercentPipe } from '@angular/common';
const currencyPipe = new CurrencyPipe('en-US');
const percentPipe = new PercentPipe('en-US');
const price = currencyPipe.transform(1234.5, 'EUR', 'symbol', '1.2-2');
const share = percentPipe.transform(0.256, '1.1-1');
// price === '€1,234.50'
// share === '25.6%'transform returns null for null or undefined input. Handle that case if your code expects a string.
Exercise
A product's discount is stored as 0.125 and its price as 49.9 in a component. Write the template expressions that show the discount as "12.5%" and the price in euros as "€49.90" in the en-US locale. Then say what {{ 0.125 | percent }} would show and why.
Show answer
Use {{ discount | percent:'1.1-1' }} for the discount and {{ price | currency:'EUR' }} for the price. The euro has two fraction digits by default, so '1.2-2' is optional. Without a digits string, {{ 0.125 | percent }} uses the default 1.0-0, so it rounds to a whole number and shows "13%" (12.5 rounds up).
Exercise
A page shows prices in Japanese yen with the expression {{ amount | currency:'JPY':'symbol':'1.2-2' }}. For amount = 1500, what does it show, and what would it show if you removed the digits string? Which version is better for yen?
Show answer
With '1.2-2' it shows "¥1,500.00", because the string forces two fraction digits. Without the digits string, the pipe uses the currency's own rule, which is zero fraction digits for yen, so it shows "¥1,500". The second version is better, because yen amounts aren't written with decimals.