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][-region]-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"best fit". 有关此选项的信息, 请参见Intl page.style"percent"表示百分比格式; 默认值是 "decimal".currency"USD" 表示美元, "EUR" 表示欧元, or "CNY"是人民币 — 更多请参考 Current currency & funds code list. 没有默认值,如果样式是“currency”,必须提供货币属性.currencyDisplay"symbol"表示使用本地化的货币符号例如€, "code"表示使用国际标准组织货币代码, "name" 表示使用本地化的货币名称,如 "dollar"; 默认值是 "symbol".useGrouping下面的属性分为两组:minimumintegerdigits,minimumfractiondigits,maximumfractiondigits作为一组,minimumsignificantdigits和maximumsignificantdigits作为一组.如果定义了第二组中的任意一个属性,则忽略第一组的设置.
minimumIntegerDigitsminimumFractionDigitsmaximumFractionDigitsminimumSignificantDigitsmaximumSignificantDigitsIntl.NumberFormat.prototypeIntl.NumberFormat.supportedLocalesOf()NumberFormat 实例NumberFormat 实例继承一下原型的属性:
Intl.NumberFormat.prototype.constructorIntl.NumberFormat.Intl.NumberFormat.prototype.formatNumberFormat object.NumberFormat 实例继承一下原型的方法:
Intl.NumberFormat.prototype.formatToParts()Array of objects representing the number string in parts that can be used for custom locale-aware formatting.Intl.NumberFormat.prototype.resolvedOptions()在不指定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
| 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 | 4.4 | 56 | 54.0 (54) | 11 | 37 | 10.2 |