The formatRange() method of Intl.NumberFormat instances formats a range of numbers according to the locale and formatting options of this Intl.NumberFormat object.
Intl.NumberFormat.prototype.formatRange()
Baseline 2023
Newly available
Since August 2023, this feature works across the latest devices and browser versions. This feature might not work in older devices or browsers.
Syntax
formatRange(startRange, endRange)
Parameters
startRange-
A
Number,BigInt, or string, to format. Strings are parsed in the same way as in number conversion, except thatformatRange()will use the exact value that the string represents, avoiding loss of precision during implicitly conversion to a number. endRange
Return value
A string representing the given range of numbers formatted according to the locale and formatting options of this Intl.NumberFormat object.
Exceptions
RangeError-
Thrown if either
startRangeorendRangeisNaNor an inconvertible string. TypeError-
Thrown if either
startRangeorendRangeis undefined.
Description
The formatRange getter function formats a range of numbers into a string according to the locale and formatting options of this Intl.NumberFormat object from which it is called.
Examples
Using formatRange
Use the formatRange getter function for formatting a range of currency values:
const nf = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
maximumFractionDigits: 0,
});
console.log(nf.formatRange(3, 5)); // "$3 – $5"
// Note: the "approximately equals" symbol is added if
// startRange and endRange round to the same values.
console.log(nf.formatRange(2.9, 3.1)); // "~$3"
const nf = new Intl.NumberFormat("es-ES", {
style: "currency",
currency: "EUR",
maximumFractionDigits: 0,
});
console.log(nf.formatRange(3, 5)); // "3-5 €"
console.log(nf.formatRange(2.9, 3.1)); // "~3 €"
Specifications
| Specification |
|---|
| ECMAScript® 2026 Internationalization API Specification # sec-intl.numberformat.prototype.formatrange |
Browser compatibility
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | Deno | Node.js | |
formatRange |
106 | 106 | 116 | 92 | 15.4 | 106 | 116 | 72 | 15.4 | 20.0 | 106 | No | 19.0.0 |
See also
© 2005–2024 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/formatRange