Skip to content

Function.formatNumberToParts

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

@sumup-oss/intl / formatNumberToParts

Function: formatNumberToParts()

formatNumberToParts(value, locales?, options?): NumberFormatPart[]

Formats a number with support for various styles, units, and notations.

Parameters

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

Returns

NumberFormatPart[]

Example

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

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

formatNumberToParts(-0.89, ['ban', 'id']);
// [
//   { type: "minusSign", value: "-" },
//   { type: "integer", value: "0" },
//   { type: "decimal", value: "," },
//   { type: "fraction", value: "89" },
// ]

formatNumberToParts(16, 'en-GB', {
  style: 'unit',
  unit: 'liter',
  unitDisplay: 'long',
});
// [
//   { type: "integer", value: "16" },
//   { type: "literal", value: " " },
//   { type: "unit", value: "litres" },
// ]

Remarks

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

Defined in

lib/number-format/index.ts:153