Intl.NumberFormat()
constructor creates objects that enable language sensitive number formatting.
new Intl.NumberFormat([locales[, options]])
locales
可选
A string with a BCP 47 language tag, or an array of such strings. For the general form and interpretation of the
locales
argument, see the
Intl page
. The following Unicode extension key is allowed:
nu
adlm
", "
ahom
", "
arab
", "
arabext
", "
bali
", "
beng
", "
bhks
", "
brah
", "
cakm
", "
cham
", "
deva
", "
diak
", "
fullwide
", "
gong
", "
gonm
", "
gujr
", "
guru
", "
hanidec
", "
hmng
", "
hmnp
", "
java
", "
kali
", "
khmr
", "
knda
", "
lana
", "
lanatham
", "
laoo
", "
latn
", "
lepc
", "
limb
", "
mathbold
", "
mathdbl
", "
mathmono
", "
mathsanb
", "
mathsans
", "
mlym
", "
modi
", "
mong
", "
mroo
", "
mtei
", "
mymr
", "
mymrshan
", "
mymrtlng
", "
newa
", "
nkoo
", "
olck
", "
orya
", "
osma
", "
rohg
", "
saur
", "
segment
", "
shrd
", "
sind
", "
sinh
", "
sora
", "
sund
", "
takr
", "
talu
", "
tamldec
", "
telu
", "
thai
", "
tibt
", "
tirh
", "
vaii
", "
wara
", "
wcho
".
options
可选
An object with some or all of the following properties:
compactDisplay
notation
is "
compact
". Takes either "
short
" (default) or "
long
".
currency
USD
" for the US dollar, "
EUR
" for the euro, or "
CNY
" for the Chinese RMB — see the
Current currency & funds code list
. There is no default value; if the
style
is "
currency
", the
currency
property must be provided.
currencyDisplay
symbol
" to use a localized currency symbol such as €, this is the default value,
narrowSymbol
" to use a narrow format symbol ("$100" rather than "US$100"),
code
" to use the ISO currency code,
name
" to use a localized currency name such as "
dollar
",
currencySign
currencySign
option to "
accounting
". The default value is "
standard
".
localeMatcher
lookup
" and "
best fit
"; the default is "
best fit
". For information about this option, see the
Intl page
.
notation
standard
"
standard
" plain number formatting
scientific
" return the order-of-magnitude for formatted number.
engineering
" return the exponent of ten when divisible by three
compact
" string representing exponent, defaults is using the "short" form.
numberingSystem
arab
", "
arabext
", "
bali
", "
beng
", "
deva
", "
fullwide
", "
gujr
", "
guru
", "
hanidec
", "
khmr
", "
knda
", "
laoo
", "
latn
", "
limb
", "
mlym
", "
mong
", "
mymr
", "
orya
", "
tamldec
", "
telu
", "
thai
", "
tibt
".
signDisplay
auto
"
auto
" sign display for negative numbers only
never
" never display sign
always
" always display sign
exceptZero
" sign display for positive and negative numbers, but not zero
style
decimal
".
decimal
" for plain number formatting.
currency
" for currency formatting.
percent
" for percent formatting
unit
" for unit formatting
unit
unit
formatting, Possible values are core unit identifiers, defined in
UTS #35, Part 2, Section 6
. A
subset
of units from the
full list
was selected for use in ECMAScript. Pairs of simple units can be concatenated with "
-per-
" to make a compound unit. There is no default value; if the
style
is "
unit
", the
unit
property must be provided.
unitDisplay
unit
formatting, the defaults is "
short
".
long
" (e.g.,
16 litres
)
short
" (e.g.,
16 l
)
narrow
" (e.g.,
16l
)
useGrouping
Whether to use grouping separators, such as thousands separators or thousand/lakh/crore separators. Possible values are true and false; the default is true.
The following properties fall into two groups:
minimumIntegerDigits
,
minimumFractionDigits
,和
maximumFractionDigits
in one group,
minimumSignificantDigits
and
maximumSignificantDigits
in the other. If at least one property from the second group is defined, then the first group is ignored.
minimumIntegerDigits
The minimum number of integer digits to use. Possible values are from 1 to 21; the default is 1.
minimumFractionDigits
maximumFractionDigits
minimumFractionDigits
and 3; the default for currency formatting is the larger of
minimumFractionDigits
and the number of minor unit digits provided by the
ISO 4217 currency code list
(2 if the list doesn't provide that information); the default for percent formatting is the larger of
minimumFractionDigits
and 0.
minimumSignificantDigits
The minimum number of significant digits to use. Possible values are from 1 to 21; the default is 1.
maximumSignificantDigits
The maximum number of significant digits to use. Possible values are from 1 to 21; the default is 21.
In basic use without specifying a locale, a formatted string in the default locale and with default options is returned.
let amount = 3500; console.log(new Intl.NumberFormat().format(amount)); // → '3,500' if in US English locale
let amount = 3500;
new Intl.NumberFormat('en-US', {style: 'decimal'}).format(amount);
// → '3,500'
new Intl.NumberFormat('en-US', {style: 'percent'}).format(amount);
// → '350,000%'
若
style
is
'unit'
,
unit
property must be provided. Optionally,
unitDisplay
controls the unit formatting.
let amount = 3500;
new Intl.NumberFormat('en-US', {style: 'unit', unit: 'liter'}).format(amount);
// → '3,500 L'
new Intl.NumberFormat('en-US', {style: 'unit', unit: 'liter', unitDisplay: 'long'}).format(amount);
// → '3,500 liters'
若
style
is
'currency'
,
currency
property must be provided. Optionally,
currencyDisplay
and
currencySign
control the unit formatting.
let amount = -3500;
new Intl.NumberFormat('en-US', {style: 'currency', currency: 'USD'}).format(amount);
// → '-$3,500.00'
new Intl.NumberFormat('bn', {
style: 'currency',
currency: 'USD',
currencyDisplay: 'name'
}).format(amount);
// → '-3,500.00 US dollars'
new Intl.NumberFormat('bn', {
style: 'currency',
currency: 'USD',
currencySign: 'accounting'
}).format(amount);
// → '($3,500.00)'
Scientific and compact notation are represented by the
notation
option and can be formatted like this:
new Intl.NumberFormat('en-US', { notation: "scientific" }).format(987654321);
// → 9.877E8
new Intl.NumberFormat('pt-PT', { notation: "scientific" }).format(987654321);
// → 9,877E8
new Intl.NumberFormat('en-GB', { notation: "engineering" }).format(987654321);
// → 987.654E6
new Intl.NumberFormat('de', { notation: "engineering" }).format(987654321);
// → 987,654E6
new Intl.NumberFormat('zh-CN', { notation: "compact" }).format(987654321);
// → 9.9亿
new Intl.NumberFormat('fr', {
notation: "compact",
compactDisplay: "long"
}).format(987654321);
// → 988 millions
new Intl.NumberFormat('en-GB', {
notation: "compact",
compactDisplay: "short"
}).format(987654321);
// → 988M
Display a sign for positive and negative numbers, but not zero:
new Intl.NumberFormat("en-US", {
style: "percent",
signDisplay: "exceptZero"
}).format(0.55);
// → '+55%'
Note that when the currency sign is "accounting", parentheses might be used instead of a minus sign:
new Intl.NumberFormat('bn', {
style: 'currency',
currency: 'USD',
currencySign: 'accounting',
signDisplay: 'always'
}).format(-3500);
// → '($3,500.00)'
| 规范 |
|---|
|
ECMAScript 国际化 API (ECMA-402)
The definition of 'Intl.NumberFormat constructor' in that specification. |
| Desktop | Mobile | Server | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
NumberFormat()
构造函数
|
Chrome 24 | Edge 12 | Firefox 29 | IE 11 | Opera 15 | Safari 10 | WebView Android 4.4 | Chrome Android 25 | Firefox Android 56 | Opera Android 14 | Safari iOS 10 | Samsung Internet Android 1.5 |
nodejs
13.0.0
|
compactDisplay
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
currencySign
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
notation
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
signDisplay
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
unit
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
unitDisplay
option
|
Chrome 77 | Edge 79 | Firefox 78 | IE No | Opera 64 | Safari No | WebView Android 77 | Chrome Android 77 | Firefox Android No | Opera Android 55 | Safari iOS No | Samsung Internet Android 12.0 | nodejs 12.11.0 |
完整支持
不支持
见实现注意事项。
Intl
Collator
DateTimeFormat
ListFormat
NumberFormat
PluralRules
RelativeTimeFormat
Object
Object.prototype.__defineGetter__()
Object.prototype.__defineSetter__()
Object.prototype.__lookupGetter__()
Object.prototype.__lookupSetter__()
Object.prototype.hasOwnProperty()
Object.prototype.isPrototypeOf()
Object.prototype.propertyIsEnumerable()
Object.prototype.toLocaleString()
Object.prototype.toSource()
Object.prototype.toString()
Object.prototype.valueOf()
Object.setPrototypeOf()