Intl.NumberFormat

Baseline 广泛可用 *

此功能已成熟,可跨多种设备和浏览器版本使用。自 2017 年 9 月以来,它已在浏览器中提供。

* 此特性的某些部分可能存在不同级别的支持。

Intl.NumberFormat 对象支持进行区域语言敏感的数字格式化。

试一试

const number = 123456.789;

console.log(
  new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
    number,
  ),
);
// Expected output: "123.456,79 €"

// The Japanese yen doesn't use a minor unit
console.log(
  new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
    number,
  ),
);
// Expected output: "¥123,457"

// Limit to three significant digits
console.log(
  new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
    number,
  ),
);
// Expected output: "1,23,000"

构造函数

Intl.NumberFormat()

创建一个新的 NumberFormat 对象。

静态方法

Intl.NumberFormat.supportedLocalesOf()

返回一个数组,其中包含提供的区域设置中受支持的那些区域设置,而无需回退到运行时默认区域设置。

实例属性

这些属性定义在 Intl.NumberFormat.prototype 上,并被所有 Intl.NumberFormat 实例共享。

Intl.NumberFormat.prototype.constructor

创建了实例对象的构造函数。对于 Intl.NumberFormat 实例,初始值为 Intl.NumberFormat 构造函数。

Intl.NumberFormat.prototype[Symbol.toStringTag]

[Symbol.toStringTag] 属性的初始值为字符串 "Intl.NumberFormat"。此属性用于 Object.prototype.toString()

实例方法

Intl.NumberFormat.prototype.format()

getter 函数,根据此 Intl.NumberFormat 对象的区域语言和格式化选项格式化数字。

Intl.NumberFormat.prototype.formatRange()

getter 函数,根据调用该方法的 Intl.NumberFormat 对象的区域语言和格式化选项格式化数字范围。

Intl.NumberFormat.prototype.formatRangeToParts()

返回一个表示数字范围字符串的对象的 Array,这些部分可用于自定义区域语言感知格式化。

Intl.NumberFormat.prototype.formatToParts()

返回一个表示数字字符串的对象的 Array,这些部分可用于自定义区域语言感知格式化。

Intl.NumberFormat.prototype.resolvedOptions()

返回一个新的对象,其中包含在对象初始化期间计算出的区域设置和排序选项。

示例

基本用法

在不指定区域设置的基本使用中,将返回采用默认区域设置和默认选项的格式化字符串。

js
const number = 3500;

console.log(new Intl.NumberFormat().format(number));
// '3,500' if in US English locale

使用语言环境

此示例展示了本地化数字格式的一些变化。为了获得应用程序用户界面中使用的语言的格式,请务必使用 locales 参数指定该语言(以及可能的备用语言)。

js
const number = 123456.789;

// German uses comma as decimal separator and period for thousands
console.log(new Intl.NumberFormat("de-DE").format(number));
// 123.456,789

// Arabic in most Arabic speaking countries uses real Arabic digits
console.log(new Intl.NumberFormat("ar-EG").format(number));
// ١٢٣٤٥٦٫٧٨٩

// India uses thousands/lakh/crore separators
console.log(new Intl.NumberFormat("en-IN").format(number));
// 1,23,456.789

// the nu extension key requests a numbering system, e.g. Chinese decimal
console.log(new Intl.NumberFormat("zh-Hans-CN-u-nu-hanidec").format(number));
// 一二三,四五六.七八九

// when requesting a language that may not be supported, such as
// Balinese, include a fallback language, in this case Indonesian
console.log(new Intl.NumberFormat(["ban", "id"]).format(number));
// 123.456,789

使用选项

可以使用 options 参数自定义结果

js
const number = 123456.789;

// request a currency format
console.log(
  new Intl.NumberFormat("de-DE", { style: "currency", currency: "EUR" }).format(
    number,
  ),
);
// 123.456,79 €

// the Japanese yen doesn't use a minor unit
console.log(
  new Intl.NumberFormat("ja-JP", { style: "currency", currency: "JPY" }).format(
    number,
  ),
);
// ¥123,457

// limit to three significant digits
console.log(
  new Intl.NumberFormat("en-IN", { maximumSignificantDigits: 3 }).format(
    number,
  ),
);
// 1,23,000

// Formatting with units
console.log(
  new Intl.NumberFormat("pt-PT", {
    style: "unit",
    unit: "kilometer-per-hour",
  }).format(50),
);
// 50 km/h

console.log(
  (16).toLocaleString("en-GB", {
    style: "unit",
    unit: "liter",
    unitDisplay: "long",
  }),
);
// 16 litres

有关选项的详尽列表,请参阅 Intl.NumberFormat() 构造函数页面。

规范

规范
ECMAScript® 2026 国际化 API 规范
# numberformat-objects

浏览器兼容性

另见