JS Intl.NumberFormat

🌙
手机阅读
本文目录结构

Intl.NumberFormat 是对语言敏感的格式化数字类的构造器类

语法

new Intl.NumberFormat([locales[, options]])
Intl.NumberFormat.call(this[, locales[, options]])

参数

locales

可选。缩写语言代码 (BCP 47 language tag, 例如:cmn-Hans-CN) 的字符串或者这些字符串组成的数组。关于参数 locales 的一般形式和解释请参见 Intl page. 下面的这些 Unicode 扩展键也是被允许的:

译者注:下面扩展的使用方式是language-scripts-u-nu-*,例如:zh-u-nu-hanidec(表示中文十进制数字)

nu

要使用的编号系统。可能的值有:“arab”, “arabext”, “bali”, “beng”, “deva”, “fullwide”, “gujr”, “guru”, “hanidec”(中文十进制数字), “khmr”, “knda”, “laoo”, “latn”, “limb”, “mlym”, “mong”, “mymr”, “orya”, “tamldec”, “telu”, “thai”, “tibt”.

options

可选。包含一些或所有的下面属性的类:

localeMatcher

使用的 local 的匹配算法。可能的值有"lookup"和"best fit"; 默认值是 “best fit”. 有关此选项的信息,请参见 Intl page.

style

格式化时使用的样式。可能的值有“decimal”表示纯数字格式 , “currency”表示货币格式,和"percent"表示百分比格式;默认值是 “decimal”.

currency

在货币格式化中使用的货币符号。可能的值是 ISO 的货币代码 (the ISO 4217 currency codes,) 例如"USD" 表示美元,“EUR” 表示欧元,or “CNY"是人民币 — 更多请参考 Current currency & funds code list. 没有默认值,如果样式是“currency”, 必须提供货币属性。

currencyDisplay

如何在货币格式化中显示货币。可能的值有"symbol"表示使用本地化的货币符号例如€, “code"表示使用国际标准组织货币代码,“name” 表示使用本地化的货币名称,如 “dollar”; 默认值是 “symbol”.

useGrouping

是否使用分组分隔符,如千位分隔符或千 / 万 / 亿分隔符。可能的值是 true 和 false, 默认值是 true. 下面的属性分为两组:minimumintegerdigits,minimumfractiondigits,maximumfractiondigits 作为一组,minimumsignificantdigits 和 maximumsignificantdigits 作为一组。如果定义了第二组中的任意一个属性,则忽略第一组的设置。

minimumIntegerDigits

使用的整数数字的最小数目。可能的值是从 1 到 21, 默认值是 1.

minimumFractionDigits

使用的小数位数的最小数目。可能的值是从 0 到 20; 默认为普通的数字和百分比格式为 0; 默认为货币格式是由国际标准化组织列表 ( ISO 4217 currency code list ) 提供(如果列表中没有提供则值为 2).

maximumFractionDigits

使用的小数位数的最大数目。可能的值是从 0 到 20;纯数字格式的默认值是 minimumfractiondigits 和 3 中大的那一个;货币格式默认值是 minimumfractiondigits 和国际标准化组织列表(如果列表中没有提供则值为 2) 中大的那一个;百分比格式默认值是 minimumfractiondigits 和 0 中大的那一个。

minimumSignificantDigits

使用的有效数字的最小数目。可能的值是从 1 到 21;默认值是 1。

maximumSignificantDigits

使用的有效数字的最大数量。可能的值是从 1 到 21;默认是 minimumsignificantdigits.

描述

属性

Intl.NumberFormat.prototype

允许将属性添加到所有对象上。

方法

Intl.NumberFormat.supportedLocalesOf()

返回一个数组包含的那些被提供的可以被支持的 locales, 运行时的默认的 locale 不会出现在该数组中。

NumberFormat 实例

属性

NumberFormat 实例继承一下原型的属性:

Intl.NumberFormat.prototype.constructor

A reference to Intl.NumberFormat.

方法

NumberFormat 实例继承一下原型的方法:

Intl.NumberFormat.prototype.format()

Getter function that formats a number according to the locale and formatting options of this NumberFormat object.

Intl.NumberFormat.prototype.formatToParts()

Returns an 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.

例子

基本用法

在不指定 locale 的基本用法中,一个使用默认 locale 和默认 options 的字符串被返回。

var number = 3500;

console.log(new Intl.NumberFormat().format(number));
// 如果在美国英语地区 → '3,500'

使用 locales

这个例子显示了一些本地化的数字格式的一些变化。为了获得用于您的应用程序的用户界面的语言格式,请确保设定了语言(可能还有一些回退语言)参数:

var number = 123456.789;

// 德语使用逗号作为小数点,使用.作为千位分隔符
console.log(new Intl.NumberFormat('de-DE').format(number));
// → 123.456,789

// 大多数阿拉伯语国家使用阿拉伯语数字
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

// 通过编号系统中的nu扩展键请求, 例如中文十进制数字
console.log(new Intl.NumberFormat('zh-Hans-CN-u-nu-hanidec').format(number));
// → 一二三,四五六.七八九

//当请求的语言不被支持,例如巴里,包含一个回滚语言印尼,这时候就会使用印尼语
console.log(new Intl.NumberFormat(['ban', 'id']).format(number));
// → 123.456,789

可用的选项

可用的选项参数,返回的值可以被设置成想要的形式:

var number = 123456.789;

// 请求一个货币格式
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

// 只显示三个有效数字
console.log(new Intl.NumberFormat('en-IN', { maximumSignificantDigits: 3 }).format(number));
// → 1,23,000

规范

规范版本 规范状态 注解
ECMAScript Internationalization API 1.0 (ECMA-402)
Intl.NumberFormat
Standard 初始定义
ECMAScript Internationalization API 2.0 (ECMA-402)
Intl.NumberFormat
Standard
ECMAScript Internationalization API 4.0 (ECMA-402)
Intl.NumberFormat
Draft

AXIHE / 精选资源

浏览全部教程

面试题

学习网站

前端培训
自己甄别

前端书籍

关于朱安邦

我叫 朱安邦,阿西河的站长,在杭州。

以前是一名平面设计师,后来开始接接触前端开发,主要研究前端技术中的JS方向。

业余时间我喜欢分享和交流自己的技术,欢迎大家关注我的 Bilibili

关注我: Github / 知乎

于2021年离开前端领域,目前重心放在研究区块链上面了

我叫朱安邦,阿西河的站长

目前在杭州从事区块链周边的开发工作,机械专业,以前从事平面设计工作。

2014年底脱产在老家自学6个月的前端技术,自学期间几乎从未出过家门,最终找到了满意的前端工作。更多>

于2021年离开前端领域,目前从事区块链方面工作了