How do you format dates, numbers, and currency correctly for different locales in an Angular app?
Short answer
Angular provides built in pipes, like date, number, and currency, that automatically format values according to the active locale, showing dates, decimal separators, and currency symbols in the style that users from that specific region expect to see.
Advertisement
AModel answer
Angular provides built in pipes, like date, number, and currency, that automatically format values according to the active locale, showing dates, decimal separators, and currency symbols in the style that users from that specific region expect to see.
<p>{{ orderDate | date:'longDate' }}</p>
<p>{{ price | currency:'EUR' }}</p>
// Formats differently depending on the app's configured locale, such as en-US versus fr-FRReal-world example
An online store shows prices with a comma as the decimal separator for French customers and a period for American customers, entirely automatically, thanks to Angular's built in currency pipe respecting the active locale.
Likely follow-ups
- How do you change the active locale used by these pipes for a specific user?
- What is the difference between formatting a value with a pipe versus doing it manually in TypeScript?
Related topics