The Intl.NumberFormat()
constructor creates Intl.NumberFormat
objects that enable language-sensitive number formatting.
Intl.NumberFormat() constructor
Try it
Syntax
new Intl.NumberFormat() new Intl.NumberFormat(locales) new Intl.NumberFormat(locales, options)
Parameters
-
locales
Optional -
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
-
The numbering system to be used. Possible values include: "
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
". — see the standard Unicode numeral systems list.
-
-
options
Optional -
An object with some or all of the following properties:
-
compactDisplay
-
Only used when
notation
is "compact
". Takes either "short
" (default) or "long
". -
currency
-
The currency to use in currency formatting. Possible values are the ISO 4217 currency codes, such as "
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 thestyle
is "currency
", thecurrency
property must be provided. -
currencyDisplay
-
How to display the currency in currency formatting. Possible values are:
- "
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
-
In many locales, accounting format means to wrap the number with parentheses instead of appending a minus sign. You can enable this formatting by setting the
currencySign
option to "accounting
". The default value is "standard
". -
localeMatcher
-
The locale matching algorithm to use. Possible values are "
lookup
" and "best fit
"; the default is "best fit
". For information about this option, see the Intl page. -
notation
-
The formatting that should be displayed for the number, the defaults is "
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 to using the "short" form.
- "
-
numberingSystem
-
Numbering System. Possible values include: "
arab
", "arabext
", "bali
", "beng
", "deva
", "fullwide
", "gujr
", "guru
", "hanidec
", "khmr
", "knda
", "laoo
", "latn
", "limb
", "mlym
", "mong
", "mymr
", "orya
", "tamldec
", "telu
", "thai
", "tibt
". -
signDisplay
-
When to display the sign for the number; defaults to "
auto
":- "
always
" always display sign - "
auto
" sign display for negative numbers only - "
exceptZero
" sign display for positive and negative numbers, but not zero - "
negative
" sign display for negative numbers only, excluding negative zero. Experimental - "
never
" never display sign
- "
-
style
-
The formatting style to use , the default is "
decimal
".- "
decimal
" for plain number formatting. - "
currency
" for currency formatting. - "
percent
" for percent formatting - "
unit
" for unit formatting
- "
-
unit
-
The unit to use in
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 thestyle
is "unit
", theunit
property must be provided. -
unitDisplay
-
The unit formatting style to use in
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. The default is
auto
.- "
always
": display grouping separators even if the locale prefers otherwise - "
auto
": display grouping separators based on the locale preference, which may also be dependent on the currency - "
false
": do not display grouping separators - "
min2
": display grouping separators when there are at least 2 digits in a group - "
true
": alias foralways
- "
-
roundingMode
-
Options for rounding modes reflecting the ICU user guide. The default is
halfExpand
.- "
ceil
": toward +∞ - "
floor
": toward -∞ - "
expand
": away from 0 - "
trunc
": toward 0 - "
halfCeil
": ties toward +∞ - "
halfFloor
": ties toward -∞ - "
halfExpand
": ties away from 0 - "
halfTrunc
": ties toward 0 - "
halfEven
": ties toward the value with even cardinality
- "
-
roundingPriority
-
Options for control rounding behavior:
- "
auto
": the significant digits always win a conflict - "
morePrecision
": the result with more precision wins a conflict - "
lessPrecision
": the result with less precision wins a conflict
- "
-
roundingIncrement
-
Specifies the rounding-increment precision. Must be one of the following integers: "
1
", "2
", "5
", "10
", "20
", "25
", "50
", "100
", "200
", "250
", "500
", "1000
", "2000
", "2500
", "5000
".Note: The
roundingIncrement
option controls the rounding increment to be used when formatting numbers:- It indicates the increment at which rounding should take place relative to the calculated rounding magnitude.
- It cannot be mixed with significant-digits rounding or any setting of
roundingPriority
other thanauto
.
For example, if
maximumFractionDigits
is 2 androundingIncrement
is 5, then the number is rounded to the nearest 0.05 ("nickel rounding").const nf = new Intl.NumberFormat("en-US", { style: "currency", currency: "USD", maximumFractionDigits: 2, roundingIncrement: 5 }); console.log(nf.format(11.29)); // > output: "$11.30" console.log(nf.format(11.25)); // > output: "$11.25" console.log(nf.format(11.22)); // > output: "$11.20"
If you set
minimumFractionDigits
andmaximumFractionDigits
, they must set them to the same value; otherwise aRangeError
is thrown. -
trailingZeroDisplay
-
A string expressing the strategy for displaying trailing zeros on whole numbers. The default is "
auto
".- "
auto
": keep trailing zeros according to minimumFractionDigits and minimumSignificantDigits - "
stripIfInteger
": the result with more precision wins a conflict - "
lessPrecision
": same as "auto", but remove the fraction digits if they are all zero
- "
The following properties fall into two groups:
minimumIntegerDigits
,minimumFractionDigits
, andmaximumFractionDigits
in one group,minimumSignificantDigits
andmaximumSignificantDigits
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
-
The minimum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number and percent formatting is 0; the default for currency formatting is the number of minor unit digits provided by the ISO 4217 currency code list (2 if the list doesn't provide that information).
-
maximumFractionDigits
-
The maximum number of fraction digits to use. Possible values are from 0 to 20; the default for plain number formatting is the larger of
minimumFractionDigits
and 3; the default for currency formatting is the larger ofminimumFractionDigits
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 ofminimumFractionDigits
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.
-
Examples
Basic usage
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
Decimal and percent formatting
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%'
Unit formatting
If the style
is 'unit'
, a 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'
Currency formatting
If the style
is 'currency'
, a 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, engineering or compact notations
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
Displaying signs
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)'
Specifications
Browser compatibility
Desktop | Mobile | Server | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | Deno | Node.js | |
NumberFormat |
24
|
12
|
29
|
11
|
15
|
10
|
4.4
|
25
|
56
|
14
|
10
|
1.5
|
1.8
|
13.0.0
0.12.0
Before version 13.0.0, only the locale data for
en-US is available by default. When other locales are specified, the
NumberFormat instance silently falls back to
en-US . To make full ICU (locale) data available before version 13, see
Node.js documentation on the --with-intl option and how to provide the data.
|
options_compactDisplay_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
options_currencyDisplay_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
10-14.1
Doesn't support
currencyDisplay: 'narrowSymbol' .
|
77
|
77
|
79
|
55
|
14.5
10-14.5
Doesn't support
currencyDisplay: 'narrowSymbol' .
|
12.0
|
1.8
|
12.11.0
|
options_currencySign_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
options_notation_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
options_signDisplay_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
options_unit_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
options_unitDisplay_parameter |
77
|
79
|
78
|
No
|
64
|
14.1
|
77
|
77
|
79
|
55
|
14.5
|
12.0
|
1.8
|
12.11.0
|
See also
© 2005–2022 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/NumberFormat/NumberFormat