Intl.NumberFormat object is a constructor for objects that enable language sensitive number formatting.

The source for this interactive example is stored in a GitHub repository. If you'd like to contribute to the interactive examples project, please clone https://github.com/mdn/interactive-examples and send us a pull request.

构造函数

Intl.NumberFormat()
创建新的 NumberFormat 对象。

静态方法

Intl.NumberFormat.supportedLocalesOf()

Returns an array containing those of the provided locales that are supported without having to fall back to the runtime's default locale.

实例方法

Intl.NumberFormat.prototype.format()
Getter function that formats a number according to the locale and formatting options of this NumberFormat 对象。
Intl.NumberFormat.prototype.formatToParts()
返回 Array of objects representing the number string in parts that can be used for custom locale-aware formatting.
Intl.NumberFormat.prototype.resolvedOptions()

Returns a new object with properties reflecting the locale and collation options computed during initialization of the object.

范例

基本用法

In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.

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

Using locales

This example shows some of the variations in localized number formats. In order to get the format of the language used in the user interface of your application, make sure to specify that language (and possibly some fallback languages) using the locales argument:

var 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
					

Using options

The results can be customized using the options argument:

var 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
					

Using style and unit

console.log(new Intl.NumberFormat("pt-PT",  {
    style: 'unit',
    unit: "mile-per-hour"
}).format(50));
// → 50 mi/h
console.log((16).toLocaleString('en-GB', {
    style: "unit",
    unit: "liter",
    unitDisplay: "long"
}));
// → 16 litres
					

规范

规范
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.NumberFormat' in that specification.

浏览器兼容性

The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out https://github.com/mdn/browser-compat-data and send us a pull request. 更新 GitHub 上的兼容性数据
Desktop Mobile Server
Chrome Edge Firefox Internet Explorer Opera Safari Android webview Chrome for Android Firefox for Android Opera for Android Safari on iOS Samsung Internet Node.js
NumberFormat Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() 构造函数 了解更多细节。
NumberFormat() 构造函数 Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 13.0.0
13.0.0
部分支持 0.12
Before version 13.0.0, only the locale data for en-US is available by default. When other locales are specified, the NumberFormat instance silently falls back to en-US . To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.
format Chrome 24 Edge 12
12
Before Edge 18, numbers are rounded to 15 decimal digits. For example, new Intl.NumberFormat('en-US').format(1000000000000005) 返回 "1,000,000,000,000,010" .
Firefox 29 IE 11
11
In Internet Explorer 11, numbers are rounded to 15 decimal digits. For example, new Intl.NumberFormat('en-US').format(1000000000000005) 返回 "1,000,000,000,000,010" .
Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() 构造函数 了解更多细节。
formatToParts Chrome 64 Edge 12 Firefox 58 IE No Opera 51 Safari 13 WebView Android 64 Chrome Android 64 Firefox Android 58 Opera Android 47 Safari iOS 13 Samsung Internet Android 9.0 nodejs 10.0.0
10.0.0
Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() 构造函数 了解更多细节。
resolvedOptions Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 0.12
0.12
Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() 构造函数 了解更多细节。
supportedLocalesOf Chrome 24 Edge 12 Firefox 29 IE 11 Opera 15 Safari 10 WebView Android 4.4 Chrome Android 25 Firefox Android 56 Opera Android 14 Safari iOS 10 Samsung Internet Android 1.5 nodejs 13.0.0
13.0.0
部分支持 0.12
Before version 13.0.0, only the locale data for en-US is available by default. To make full ICU (locale) data available for versions prior to 13, see Node.js documentation on the --with-intl option and how to provide the data.

图例

完整支持

完整支持

不支持

不支持

实验。期望将来行为有所改变。

实验。期望将来行为有所改变。

见实现注意事项。

另请参阅

元数据

  • 最后修改: