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

Function formatCurrency

packages/common/src/i18n/format_number.ts:186–218  ·  view source on GitHub ↗
(
  value: number,
  locale: string,
  currency: string,
  currencyCode?: string,
  digitsInfo?: string,
)

Source from the content-addressed store, hash-verified

184 * @publicApi
185 */
186export function formatCurrency(
187 value: number,
188 locale: string,
189 currency: string,
190 currencyCode?: string,
191 digitsInfo?: string,
192): string {
193 const format = getLocaleNumberFormat(locale, NumberFormatStyle.Currency);
194 const pattern = parseNumberFormat(format, getLocaleNumberSymbol(locale, NumberSymbol.MinusSign));
195
196 pattern.minFrac = getNumberOfCurrencyDigits(currencyCode!);
197 pattern.maxFrac = pattern.minFrac;
198
199 const res = formatNumberToLocaleString(
200 value,
201 pattern,
202 locale,
203 NumberSymbol.CurrencyGroup,
204 NumberSymbol.CurrencyDecimal,
205 digitsInfo,
206 );
207 return (
208 res
209 .replace(CURRENCY_CHAR, currency)
210 // if we have 2 time the currency character, the second one is ignored
211 .replace(CURRENCY_CHAR, '')
212 // If there is a spacing between currency character and the value and
213 // the currency character is suppressed by passing an empty string, the
214 // spacing character would remain as part of the string. Then we
215 // should remove it.
216 .trim()
217 );
218}
219
220/**
221 * @ngModule CommonModule

Callers 2

transformMethod · 0.90

Calls 6

getLocaleNumberFormatFunction · 0.90
getLocaleNumberSymbolFunction · 0.90
parseNumberFormatFunction · 0.85
replaceMethod · 0.45

Tested by

no test coverage detected