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
andoptions
are optional parameters.
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 number.
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 num = 12300var curr = num.toLocaleString('en-US', {style: 'currency', currency: 'USD'})console.log("12300 represented as dollars: ", curr)
"12300 represented as dollars: " "$12,300.00"
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.
Consider the code snippet below, which demonstrates the use of the toLocaleString()
method to format a number in locale number formats:
var num = 12300console.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'))
"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"
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.