MCPcopy Create free account
hub / github.com/angular/angular / formatNumberToLocaleString

Function formatNumberToLocaleString

packages/common/src/i18n/format_number.ts:33–159  ·  view source on GitHub ↗

* Transforms a number to a locale string based on a style and a format.

(
  value: number,
  pattern: ParsedNumberFormat,
  locale: string,
  groupSymbol: NumberSymbol,
  decimalSymbol: NumberSymbol,
  digitsInfo?: string,
  isPercent = false,
)

Source from the content-addressed store, hash-verified

31 * Transforms a number to a locale string based on a style and a format.
32 */
33function formatNumberToLocaleString(
34 value: number,
35 pattern: ParsedNumberFormat,
36 locale: string,
37 groupSymbol: NumberSymbol,
38 decimalSymbol: NumberSymbol,
39 digitsInfo?: string,
40 isPercent = false,
41): string {
42 let formattedText = '';
43 let isZero = false;
44
45 if (!isFinite(value)) {
46 // `Number.isNaN` (not `!isFinite`) so NaN uses the locale NaN symbol, not Infinity.
47 formattedText = getLocaleNumberSymbol(
48 locale,
49 Number.isNaN(value) ? NumberSymbol.NaN : NumberSymbol.Infinity,
50 );
51 } else {
52 let parsedNumber = parseNumber(value);
53
54 if (isPercent) {
55 parsedNumber = toPercent(parsedNumber);
56 }
57
58 let minInt = pattern.minInt;
59 let minFraction = pattern.minFrac;
60 let maxFraction = pattern.maxFrac;
61
62 if (digitsInfo) {
63 const parts = digitsInfo.match(NUMBER_FORMAT_REGEXP);
64 if (parts === null) {
65 throw new RuntimeError(
66 RuntimeErrorCode.INVALID_DIGIT_INFO,
67 ngDevMode && `${digitsInfo} is not a valid digit info`,
68 );
69 }
70 const minIntPart = parts[1];
71 const minFractionPart = parts[3];
72 const maxFractionPart = parts[5];
73 if (minIntPart != null) {
74 minInt = parseIntAutoRadix(minIntPart);
75 }
76 if (minFractionPart != null) {
77 minFraction = parseIntAutoRadix(minFractionPart);
78 }
79 if (maxFractionPart != null) {
80 maxFraction = parseIntAutoRadix(maxFractionPart);
81 } else if (minFractionPart != null && minFraction > maxFraction) {
82 maxFraction = minFraction;
83 }
84
85 // Prevent DoS via resource exhaustion by capping the maximum padding iterations
86 const MAX_ALLOWED_DIGITS = 100;
87 if (
88 minInt > MAX_ALLOWED_DIGITS ||
89 minFraction > MAX_ALLOWED_DIGITS ||
90 maxFraction > MAX_ALLOWED_DIGITS

Callers 3

formatCurrencyFunction · 0.85
formatPercentFunction · 0.85
formatNumberFunction · 0.85

Calls 7

getLocaleNumberSymbolFunction · 0.90
parseNumberFunction · 0.85
toPercentFunction · 0.85
roundNumberFunction · 0.85
parseIntAutoRadixFunction · 0.70
joinMethod · 0.65
matchMethod · 0.45

Tested by

no test coverage detected