Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is toLocaleString() in JavaScript?

Kainat Asif

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

toLocaleString() is a Number method that converts a number to a language-sensitive string. The toLocaleString() method is declared as follows:

num.toLocaleString(locales, options)
  • num: The number 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.

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 number.

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 num = 12300
var curr = num.toLocaleString('en-US', {style: 'currency', currency: 'USD'})
console.log("12300 represented as dollars: ", curr)

Output

"12300 represented as dollars: " "$12,300.00"

Explanation

A variable num is declared in line 1. The toLocaleString() method is called in line 3, and returns the US numeric representation of num 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 num = 12300
console.log("12300 formatted as locale number format in Hindi :", num.toLocaleString('hi-IN'))
console.log("12300 formatted as locale number format in Arabic :", num.toLocaleString('ar-SA'))
console.log("12300 formatted as locale number format in Swiss :", num.toLocaleString('it-CH'))

Output

"12300 formatted as locale number format in Hindi :" "12,300"
"12300 formatted as locale number format in Arabic :" "١٢٬٣٠٠"
"12300 formatted as locale number format in Swiss :" "12’300"

Explanation

A variable num is declared with num=12300 in line 1. The toLocaleString() method is used in line 4, line 6, and line 9 to format num as locale number format in Hindi, Arabic, and Swiss, respectively.

RELATED TAGS

javascript

CONTRIBUTOR

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

Grokking Modern System Design Interview for Engineers & Managers

Ace your System Design Interview and take your career to the next level. Learn to handle the design of applications like Netflix, Quora, Facebook, Uber, and many more in a 45-min interview. Learn the RESHADED framework for architecting web-scale applications by determining requirements, constraints, and assumptions before diving into a step-by-step design process.

Keep Exploring