Intl.NumberFormat.prototype.formatToParts
Formats a number and returns an array of objects representing the formatted number in parts
Syntax
numberFormat.formatToParts(number)Parameters
| Parameter | Type | Description |
|---|---|---|
| number | number | bigint | The number to format into parts |
Return Value
An array of objects with type and value properties
Examples
const fmt = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
const parts = fmt.formatToParts(1234.56)
console.log(parts)
// [{type:'currency',value:'$'},{type:'integer',value:'1'}, ...]function getIntegerPart(num: number, locale: string): string {
const fmt = new Intl.NumberFormat(locale)
const parts = fmt.formatToParts(num)
return parts.filter(p => p.type === 'integer').map(p => p.value).join('')
}const fmt = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' })
const parts = fmt.formatToParts(99.99)
const symbol = parts.find(p => p.type === 'currency')?.value
console.log('Currency symbol:', symbol) // '$'Understanding Intl.NumberFormat.prototype.formatToParts
The Intl.NumberFormat.prototype.formatToParts method in JavaScript formats a number and returns an array of objects representing the formatted number in parts. It belongs to the Intl.NumberFormat object and is one of the most widely used methods for working with intl.numberformat values in modern JavaScript and TypeScript applications.
The method signature is numberFormat.formatToParts(number). It accepts 1 parameter: number. When called, it returns an array of objects with type and value properties. Understanding when and how to use formatToParts() helps you write more expressive, readable code.
Common use cases for Intl.NumberFormat.prototype.formatToParts include data transformation, input validation, API response processing, and building reusable utility functions. It works well alongside related methods like intl-numberformat-format, intl-numberformat, intl-datetimeformat-formattoparts, enabling you to chain operations together for complex data manipulation pipelines.
Browser support for Intl.NumberFormat.prototype.formatToParts is excellent across all modern browsers including Chrome, Firefox, Safari, and Edge. It is also fully supported in Node.js and Deno. For older environments, transpilation with Babel or a polyfill may be needed.
Browser Compatibility
Supported in all modern browsers (Chrome, Firefox, Safari, Edge) and Node.js. Part of the ECMAScript standard.
Related Methods
Intl.NumberFormat.prototype.formatFormats a number according to the locale and formatting options of this Intl.NumberFormat object
Intl.NumberFormatCreates an Intl.NumberFormat object that enables language-sensitive number formatting
Intl.DateTimeFormat.prototype.formatToPartsFormats a date into an array of objects representing the formatted date in parts
More Intl.NumberFormat Methods
Other methods in the Intl.NumberFormat object
Related Tools
More Intl.NumberFormat Methods
Explore JavaScript Methods
Browse our complete reference of 410 JavaScript methods with syntax, examples, and explanations.