Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is Array.toLocaleString() in Javascript?

Kainat Asif

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 and options are optional parameters.
  • If the object passed is not an array, the TypeError is thrown.

Locales

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

Options

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.

Return value

The toLocaleString() method returns a string that is the language-sensitive representation of the elements of the array.

Browser compatibility

The toLocaleString() method is supported by the following browsers:

  • Edge 12
  • Firefox 1
  • Google Chrome 1
  • Opera 4
  • Safari 1

The locales and options parameters are supported by the following browsers:

  • Edge 12
  • Firefox 29
  • Google Chrome 24
  • Opera 15
  • Safari 10

Examples

Example 1

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)

Output

arr represented as dollars:  $12,300.00,$1,000.00,$1,500,000.00

Explanation

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.

Example 2

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'))

Output

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

Explanation

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.

RELATED TAGS

javascript

CONTRIBUTOR

Kainat Asif
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring