Angular Percent Pipe: Format Numbers as Percentages Easily
percent pipe in Angular formats a number as a percentage string by multiplying it by 100 and adding a percent sign. It is used in templates to display decimal numbers as readable percentages with optional control over decimal places.How It Works
The percent pipe in Angular takes a decimal number and converts it into a percentage format. Imagine you have a score like 0.85, which means 85%. The pipe multiplies this number by 100 and adds a percent sign (%) to make it easy to read.
Think of it like a kitchen scale that changes grams into kilograms for easier understanding. Similarly, the percent pipe changes decimals into percentages so users can quickly grasp the value without doing math themselves.
Example
This example shows how to use the percent pipe in an Angular template to display a decimal as a percentage with two decimal places.
<!-- app.component.html -->
<p>Completion: {{ 0.756 | percent:'1.2-2' }}</p>When to Use
Use the percent pipe whenever you want to show ratios, progress, or proportions as percentages in your app. For example, showing quiz scores, task completion rates, or financial growth percentages makes data clearer and more user-friendly.
It is especially helpful when your data is stored as decimals but your users expect to see percentages, saving you from manual calculations and formatting.
Key Points
- The
percentpipe multiplies the input by 100 and adds a percent sign. - You can control decimal places using format strings like
'1.0-2'. - It improves readability by converting decimals to user-friendly percentages.
- Works directly in Angular templates without extra code.