-
Notifications
You must be signed in to change notification settings - Fork 4
Function.formatNumberToParts
connor-baer edited this page Dec 5, 2024
·
56 revisions
@sumup-oss/intl / formatNumberToParts
formatNumberToParts(
value
,locales
?,options
?):NumberFormatPart
[]
Formats a number with support for various styles, units, and notations.
Parameter | Type |
---|---|
value |
number |
locales ? |
string | string [] |
options ? |
NumberFormatOptions |
NumberFormatPart
[]
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" },
// ]
In runtimes that don't support the Intl.NumberFormat.formatToParts
API,
the number is localized and returned as a single integer part.