Intl.NumberFormat.prototype.format() method formats a number according to the locale and formatting options of this NumberFormat 对象。

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.

句法

numberFormat.format(number)
					

参数

number
Number or BigInt to format.

描述

format getter function formats a number into a string according to the locale and formatting options of this NumberFormat 对象。

范例

使用 format

使用 format getter function for formatting a single currency value, here for Russia:

var options = { style: 'currency', currency: 'RUB' };
var numberFormat = new Intl.NumberFormat('ru-RU', options);
console.log(numberFormat.format(654321.987));
// → "654 321,99 руб."
					

使用 format with map

使用 format getter function for formatting all numbers in an array. Note that the function is bound to the NumberFormat from which it was obtained, so it can be passed directly to Array.prototype.map . This is considered a historical artefact, as part of a convention which is no longer followed for new features, but is preserved to maintain compatibility with existing programs.

var a = [123456.789, 987654.321, 456789.123];
var numberFormat = new Intl.NumberFormat('es-ES');
var formatted = a.map(n => numberFormat.format(n));
console.log(formatted.join('; '));
// → "123.456,789; 987.654,321; 456.789,123"
					

规范

规范
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.NumberFormat.prototype.format' 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
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() 构造函数 了解更多细节。

图例

完整支持

完整支持

见实现注意事项。

另请参阅

元数据

  • 最后修改: