非常教程

JavaScript参考手册

Intl

Intl.numberFormat.format

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

句法

numberFormat.format(number)

参数

number要格式化的数字。

描述

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

例子

运用 format

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

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

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

JavaScript

JavaScript 是一种高级编程语言,通过解释执行,是一门动态类型,面向对象(基于原型)的解释型语言。它已经由ECMA(欧洲电脑制造商协会)通过 ECMAScript 实现语言的标准化。它被世界上的绝大多数网站所使用,也被世界主流浏览器( Chrome、IE、FireFox、Safari、Opera )支持。JavaScript 是一门基于原型、函数先行的语言,是一门多范式的语言,它支持面向对象编程,命令式编程,以及函数式编程。它提供语法来操控文本、数组、日期以及正则表达式等,不支持 I/O,比如网络