Intl.NumberFormat.prototype.formatRange()
Intl.NumberFormat
实例的 formatRange()
方法根据此 Intl.NumberFormat
对象的区域设置和格式选项来格式化一系列数字。
¥The formatRange()
method of Intl.NumberFormat
instances formats a range of numbers according to the locale and formatting options of this Intl.NumberFormat
object.
语法
参数
返回值
¥Return value
表示给定数字范围的字符串,该数字范围根据此 Intl.NumberFormat
对象的区域设置和格式选项进行格式化。
¥A string representing the given range of numbers formatted according to the locale and formatting options of this Intl.NumberFormat
object.
例外情况
¥Exceptions
RangeError
-
如果
startRange
小于endRange
,或者任一值为NaN
,则抛出该异常。 TypeError
-
如果
startRange
或endRange
未定义,则抛出该异常。
描述
¥Description
formatRange
getter 函数根据调用它的 Intl.NumberFormat
对象的区域设置和格式选项将一系列数字格式化为字符串。
¥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.
示例
使用格式范围
¥Using formatRange
使用 formatRange
getter 函数格式化一系列货币值:
¥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 €"
规范
Specification |
---|
ECMAScript Internationalization API Specification # sec-intl.numberformat.prototype.formatrange |
浏览器兼容性
BCD tables only load in the browser