Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

javascript

What is Object.toLocaleString() in JavaScript?

Syed Muhammad Ali Mustafa

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.

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

obj.toLocaleString(locales, options)
  • obj: The object that is converted to a string.
  • locales: The language format that is used.
  • options: The options used while converting the number to a string.

Note:

  • locales and options are optional parameters.
  • The method is overridden by user defined classes. If the method is not overridden, the method returns [object Object].

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 used for
localeMatcher The locale matching algorithm. It can be best fit (default) or lookup.
style The formatting style. It can be decimal, currency or percent.
currency The currency code. For example, “USD” or “EUR”.
currencyDisplay How the currency formatting will be displayed. It can be symbol, code or name.
useGrouping Determines whether grouping separators will be used. It can be true or false.
minimumFractionDigits The minimum number of fraction digits to be displayed. It is a value in the range of 0 to 20. The default value is 3.
maximumFractionDigits The maximum number of fraction digits to be displayed. It is a value in the range of 0 to 20. The default value is 3.
minimumSignificantDigits The minimum number of significant digits to be displayed. It is a value in the range of 1 to 21. The default value is 21.
maximumSignificantDigits The maximum number of significant digits to be displayed. It is a value in the range of 1 to 21. The default value is 21.
minimumIntegerDigits The minimum number of integer digits to be displayed. It is 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 object.

Browser compatibility

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

  • Edge
  • Firefox
  • Google Chrome
  • Opera
  • Safari

Example

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)

RELATED TAGS

javascript

CONTRIBUTOR

Syed Muhammad Ali Mustafa
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