Intl.NumberFormat.prototype.formatRange()
The formatRange()
method of Intl.NumberFormat
instances formats a range of numbers according to the locale and formatting options of this Intl.NumberFormat
object.
Syntax
js
formatRange(startRange, endRange)
Parameters
Exceptions
RangeError
-
Thrown if
startRange
is less thanendRange
, or either value isNaN
. TypeError
-
Thrown if either
startRange
orendRange
is 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:
js
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"
js
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 Internationalization API Specification # sec-intl.numberformat.prototype.formatrange |
Browser compatibility
BCD tables only load in the browser