Intl.NumberFormat.prototype.format

 Intl.NumberFormat.prototype.format 属性返回一个根据NumberFormat对象的语言环境和格式化选项,来格式化一个数字的getter函数。

语法

numberFormat.format(number)

参数

number
要格式化的数值。

描述

该函数返回一个根据NumberFormat对象的语言环境和格式化选项,来format 一个数字的函数。

示例

使用 format

使用 format 格式化一个单一的货币值, 以俄罗斯为例:

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

使用 format 和 map

使用 format 返回的函数来格式化数组中的所有数字。注意,该函数绑定到所获得的NumberFormat,因此它可以直接传递给Array.prototype.map

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

规范

规范 状态 注解
ECMAScript Internationalization API 1.0 (ECMA-402)
Intl.NumberFormat.prototype.format
Standard 初始定义
ECMAScript Internationalization API 2.0 (ECMA-402)
Intl.NumberFormat.prototype.format
Standard  
ECMAScript Internationalization API (ECMA-402)
Intl.NumberFormat.prototype.format
Living Standard  

浏览器兼容性

Update compatibility data on GitHub
DesktopMobileServer
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung InternetNode.js
formatChrome Full support 24Edge Full support 12
Notes
Full support 12
Notes
Notes Before Edge 18, numbers are rounded to 15 decimal digits. For example, new Intl.NumberFormat('en-US').format(1000000000000005) returns "1,000,000,000,000,010".
Firefox Full support 29IE Full support 11
Notes
Full support 11
Notes
Notes In Internet Explorer 11, numbers are rounded to 15 decimal digits. For example, new Intl.NumberFormat('en-US').format(1000000000000005) returns "1,000,000,000,000,010".
Opera Full support 15Safari Full support 10WebView Android Full support 4.4Chrome Android Full support 25Firefox Android Full support 56Opera Android Full support 14Safari iOS Full support 10Samsung Internet Android Full support 1.5nodejs Full support 0.12
Notes
Full support 0.12
Notes
Notes Before version 13.0.0, only the locale data for en-US is available by default. See the NumberFormat() constructor for more details.

Legend

Full support  
Full support
See implementation notes.
See implementation notes.

相关链接