Skip to content

Function.formatCurrencyToParts

kodiakhq[bot] edited this page Nov 1, 2024 · 45 revisions

@sumup-oss/intl / formatCurrencyToParts

Function: formatCurrencyToParts()

formatCurrencyToParts(value, locales?, currency?, options?): NumberFormatPart[]

Formats a number in the country's official currency with support for various notations.

Parameters

Parameter Type
value number
locales? string | string[]
currency? string
options? NumberFormatOptions

Returns

NumberFormatPart[]

Example

import { formatCurrencyToParts } from '@sumup-oss/intl';

formatCurrencyToParts(12345.67, 'de-DE');
// [
//   { type: "integer", value: "12" },
//   { type: "group", value: "." },
//   { type: "integer", value: "345" },
//   { type: "decimal", value: "," },
//   { type: "fraction", value: "67" },
//   { type: "literal", value: " " },
//   { type: "currency", value: "€" },
// ]

formatCurrencyToParts(-89, 'ja-JP', 'JPY');
// [
//   { type: "minusSign", value: "-" },
//   { type: "currency", value: "¥" },
//   { type: "integer", value: "89" },
// ]

formatCurrencyToParts(16, 'en-GB', null, { currencyDisplay: 'name' });
// [
//   { type: "integer", value: "16" },
//   { type: "decimal", value: "." },
//   { type: "fraction", value: "00" },
//   { type: "literal", value: " " },
//   { type: "currency", value: "British pounds" },
// ]

Remarks

In runtimes that don't support the Intl.NumberFormat.formatToParts API, the currency is localized and returned as a single integer part.

The COP and HUF currencies are formatted without decimals.

Defined in

lib/number-format/index.ts:204