toLocaleString()
converts the elements of an array to a language-sensitive string. The toLocaleString()
method is declared as follows:
arr.toLocaleString(locales, options)
arr
: The array that is to be converted to a string.locales
: The language format to be used.options
: The options used while converting the number to a string.Note:
locales
andoptions
are optional parameters.- If the object passed is not an array, the TypeError is thrown.
A locale can be any of the following:
Locale | English |
---|---|
ar-SA | Arabic |
bn-BD | Bangla (Bangladesh) |
bn-IN | Bangla (India) |
cs-CZ | Czech |
da-DK | Danish |
de-AT | Austrian German |
de-CH | “Swiss” German |
de-DE | Standard German |
el-GR | Modern Greek |
en-AU | Australian English |
en-CA | Canadian English |
en-GB | British English |
en-IE | Irish English |
en-IN | Indian English |
en-NZ | New Zealand English |
en-US | US English |
en-ZA | South African English |
es-AR | Argentine Spanish |
es-CL | Chilean Spanish |
es-CO | Colombian Spanish |
es-ES | Castilian Spanish |
es-MX | Mexican Spanish |
es-US | American Spanish |
fi-FI | Finnish |
fr-BE | Belgian French |
fr-CA | Canadian French |
fr-CH | “Swiss” French |
Locale | English |
---|---|
fr-FR | Standard French |
he-IL | Hebrew |
hi-IN | Hindi |
hu-HU | Hungarian |
id-ID | Indonesian |
it-CH | “Swiss” Italian |
it-IT | Standard Italian |
ja-JP | Japanese |
ko-KR | Korean |
nl-BE | Belgian Dutch |
nl-NL | Standard Dutch |
no-NO | Norwegian |
pl-PL | Polish |
pt-BR | Brazilian Portuguese |
pt-PT | European Portuguese |
ro-RO | Romanian |
ru-RU | Russian |
sk-SK | Slovak |
sv-SE | Swedish |
ta-IN | Indian Tamil |
ta-LK | Sri Lankan Tamil |
th-TH | Thai |
tr-TR | Turkish |
zh-CN | Mainland China, simplified characters |
zh-HK | Hong Kong, traditional characters |
zh-TW | Taiwan, traditional characters |
The options can be any of the following:
Option | Description |
---|---|
localeMatcher | The locale matching algorithm; can be best fit (default) or lookup. |
style | The formatting style; can be decimal, currency, or percent. |
currency | The currency code. For example, “USD” or “EUR”. |
currencyDisplay | How the currency formatting will be displayed; can be symbol, code, or name. |
useGrouping | Determines whether grouping separators will be used; can be true or false. |
minimumFractionDigits | The minimum number of fraction digits to be displayed; a value in the range of 0 to 20. The default value is 3. |
maximumFractionDigits | The maximum number of fraction digits to be displayed; a value in the range of 0 to 20. The default value is 3. |
minimumSignificantDigits | The minimum number of significant digits to be displayed; a value in the range of 1 to 21. The default value is 21. |
maximumSignificantDigits | The maximum number of significant digits to be displayed; a value in the range of 1 to 21. The default value is 21. |
minimumIntegerDigits | The minimum number of integer digits to be displayed; a value in the range of 1 to 21. The default value is 1. |
The toLocaleString()
method returns a string that is the language-sensitive representation of the elements of the array.
The toLocaleString()
method is supported by the following browsers:
The locales
and options
parameters are supported by the following browsers:
Consider the code snippet below, which demonstrates the use of the toLocaleString()
method:
var arr = [12300, 1000, 1500000]var curr = arr.toLocaleString('en-US', {style: 'currency', currency: 'USD'})console.log("arr represented as dollars: ", curr)
arr represented as dollars: $12,300.00,$1,000.00,$1,500,000.00
An array arr
is declared in line 1. The toLocaleString()
method is called in line 3 and returns the US numeric representation of the elements of arr
displayed as USD currency.
Consider the code snippet below, which demonstrates the use of the toLocaleString()
method to format a number in locale number formats:
var arr = [12300, 1000, 1500000]console.log("arr formatted as locale number format in Hindi :", arr.toLocaleString('hi-IN'))console.log("arr formatted as locale number format in Arabic :", arr.toLocaleString('ar-SA'))console.log("arr formatted as locale number format in Swiss :", arr.toLocaleString('it-CH'))
arr formatted as locale number format in Hindi : 12,300,1,000,15,00,000
arr formatted as locale number format in Arabic :"١٢٬٣٠٠,١٬٠٠٠,١٬٥٠٠٬٠٠٠
arr formatted as locale number format in Swiss : 12’300,1’000,1’500’000
An array arr
is declared in line 1. The toLocaleString()
method is used in line 4, line 6, and line 9 to format the elements of arr
as locale number format in Hindi, Arabic, and Swiss, respectively.
Free Resources