NG.I · Angular 22 practice question

A developer is building a service in Angular 22 that needs to format a numeric value according to the user's locale. According to current best practices documented for this framework, what is the recommended approach for achieving locale-aware formatting?

import {inject, Inject} from '@angular/core';
import {formatNumber} from '@angular/common';

@Injectable({ providedIn: 'root' }) 
export class NumberFormatterService {
  private locale = inject(LOCALE_ID);

  formatValue(value: number) {
    return formatNumber(value, this.locale, '1.2-2');
  }
}
Choose one answer
Check answer and explanation

Correct answer: Utilizing the standalone formatting function, such as formatNumber, imported from '@angular/common'.

Explanation

The documentation explicitly advises using the standalone formatting functions, stating: "Use these functions instead of injecting the pipe class."

Common trap

Injecting the pipe class is the older pattern that the documentation advises against. Relying on the template syntax inside a service defeats the purpose of the service.

Source: Angular docs: Pipes — Use formatting functions for built-in pipes
Checked against the cited text on 2026-11-25.

Report an error in this question