首页
学习
活动
专区
工具
TVP
发布
精选内容/技术社群/优惠产品,尽在小程序
立即前往

Intl.numberFormat.format

Intl.NumberFormat.prototype.format属性返回一个getter函数,根据此NumberFormat对象的区域设置和格式化选项来格式化数字。

句法

代码语言:javascript
复制
numberFormat.format(number)

参数

number要格式化的数字。

描述

formatgetter 返回的函数根据此NumberFormat对象的区域设置和格式化选项将数字格式化为字符串。

例子

运用 format

使用formatgetter 函数返回的函数格式化单个货币值,这里是俄罗斯的:

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

使用formatmap

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

代码语言:javascript
复制
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"

产品规格

Specification

Status

Comment

ECMAScript Internationalization API 1.0 (ECMA-402)The definition of 'Intl.NumberFormat.prototype.format' in that specification.

Standard

Initial definition.

ECMAScript Internationalization API 2.0 (ECMA-402)The definition of 'Intl.NumberFormat.prototype.format' in that specification.

Standard

?

ECMAScript Internationalization API 4.0 (ECMA-402)The definition of 'Intl.NumberFormat.prototype.format' in that specification.

Draft

?

浏览器兼容性

Feature

Chrome

Firefox (Gecko)

Internet Explorer

Opera

Safari (WebKit)

Basic support

24

29 (29)

11

15

10

Feature

Android

Chrome for Android

Firefox Mobile (Gecko)

IE Phone

Opera Mobile

Safari Mobile

Basic support

No support

26

56.0 (56)

No support

No support

10

扫码关注腾讯云开发者

领取腾讯云代金券

http://www.vxiaotou.com