Number.prototype.toLocaleString() in JavaScript

Overview
Different countries follow different sets of rules to show the number count in their country, as an example- India denotes the thirty thousand points one zero(30,000.10) by separating the hundred and thousand values by comma(,) and decimal value using the period(.) symbol, but in Argentine Spanish thirty thousand will be written like this- 30.000,10 where period(.) is used to separate the hundreds and thousands and period(.) is used to separate the decimal value. To make it easy to convert a country's number count into another country's specific number count, JavaScript Number Object provides the toLocaleString method.
In this article, we will learn about the Number.prototype.toLocaleString() method and how to use it by taking some examples.
Syntax of Number.prototype.toLocaleString() in JavaScript
Syntax-
Parameters of Number.prototype.toLocaleString() in JavaScript
JavaScript toLocaleString method can be called upon a number value without passing any argument, as result, toLocaleString will format the number value by following the default setting of our local machine. But, to change the default behavior of the JavascripttoLocaleString method we can customize it by passing the following arguments:
Locales
As the first argument of the Javascript toLocaleString method takes a string value as a custom local language name. Let's take an example to understand this.
In the example below, we called JavascripttoLocaleString method upon a number value by passing a local language name as an argument-
Output-
In the example above, we applied the Javascript toLocaleString method upon the myVal variable by passing the locale language name value "da-DK" as an argument and then assigned the return value in the result variable. When we logged the result, it prints "10.000" on the console which is the formatted number value in the local danish language.
Just like we pass the "da-DK" string value for the Danish local language, string values to represent other local languages are the following:
| String value | Local language name | |
|---|---|---|
| ar-SA | Arabic (Saudi Arabia) | |
| bn-BD | Bangla (Bangladesh) | |
| bn-IN | Bangla (India) | |
| cs-CZ | Czech (Czech Republic) | |
| da-DK | Danish (Denmark) | |
| de-AT | Austrian German | |
| de-CH | "Swiss" German | |
| de-DE | Standard German (as spoken in Germany) | |
| 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 | English (South Africa) | |
| es-AR | Argentine Spanish | |
| es-CL | Chilean Spanish | |
| es-CO | Colombian Spanish | |
| es-ES | Castilian Spanish (as spoken in Central-Northern Spain) | |
| es-MX | Mexican Spanish | |
| es-US | American Spanish | |
| fi-FI | Finnish (Finland) | |
| fr-BE | Belgian French | |
| fr-CA | Canadian French | |
| fr-CH | "Swiss" French | |
| fr-FR | Standard French (especially in France) | |
| he-IL | Hebrew (Israel) | |
| hi-IN | Hindi (India) | |
| hu-HU | Hungarian (Hungary) | |
| id-ID | Indonesian (Indonesia) | |
| it-CH | "Swiss" Italian | |
| it-IT | Standard Italian (as spoken in Italy) | |
| ja-JP | Japanese (Japan) | |
| ko-KR | Korean (Republic of Korea) | |
| nl-BE | Belgian Dutch | |
| nl-NL | Standard Dutch (as spoken in The | Netherlands) |
| no-NO | Norwegian (Norway) | |
| pl-PL | Polish (Poland) | |
| pt-BR | Brazilian Portuguese | |
| pt-PT | European Portuguese (as written and spoken | in Portugal) |
| ro-RO | Romanian (Romania) | |
| ru-RU | Russian (Russian Federatio | n) |
| sk-SK | Slovak (Slovakia) | |
| sv-SE | Swedish (Sweden) | |
| ta-IN | Indian Tamil | |
| ta-LK | Sri Lankan Tamil | |
| th-TH | Thai (Thailand) | |
| tr-TR | Turkish (Turkey) | |
| zh-CN | Mainland China, simplified characters | |
| zh-HK | Hong Kong, traditional characters | |
| zh-TW | Taiwan, traditional characters |
Options
In the second argument, the Javascript toLocaleString method takes an object where we have the option to customize the currency, style, maximumFractionDigits, etc. Let's take an example to understand this.
In the example below, we called the toLocaleString method upon a number value by passing a local language name as an argument-
Output-
In the example above, we applied the JavascripttoLocaleString method upon myVal variable by passing locale language name value "pl-PL" and {style: "currency", currency: "USD"} as arguments, and then assigned the return value in the result variable. When we logged the result, it prints "20 000,00 USD" on the console which is the formatted number value in the local Polish language.
Just like we pass an object value in the second argument, we can pass these following key-value pairs as argument:
| KEY | VALUE |
|---|---|
| currency | Any currency code like- "INR", "EUR", "USD", etc. |
| style | "currency", "percent", "decimal" is default. |
| currencyDisplay | "code", "name", "symbol" is default. |
| localeMatcher | "lookup", "best-fit" is default. |
| maximumFractionDigits | Takes a number from 0 to 20, the default value is 3. |
| maximumSignificantDigits | Takes a number from 1 to 21, default is 21. |
| minimumFractionDigits | Takes a number from 0 to 20 default is 3. |
| minimumIntegerDigits | Take a number from 1 to 21 default is 1. |
| minimumSignificantDigits | Takes a number from 1 to 21 default is 21. |
| useGrouping | false, true is default. |
Return Value of Number.prototype.toLocaleString() in JavaScript
The return value of Number.prototype.toLocaleString is a formatted string value in the local language. For example, it will call it on 10000, then the return value will be the following-
Output-
In the above example, we call the toLocaleString on a variable myVal and store the return value in the result variable. Finally, When we logged the result it prints "10,000" on the console.
Examples
In this example, we will convert a number value into a local language format.
To convert a number value into local language format, we will call the toLocaleString method upon the number value we want to format like this-
Output-
In the above example, we called the toLocaleString method on the myVal variable and store the return value in the result variable, when we logged the result variable, it prints "20,000" on the console.
Just like we pass the style and currency to customize the behavior of the toLocaleString method, we can pass other attributes like- maximumFractionDigits, and minimumFractionDigits, you can see a list of available attributes in the given table above.
What is Number.prototype.toLocaleString() in JavaScript?
Javascript toLocaleString is a predefined method in the Number object. It can be called upon a value without passing any arguments, or to customize the default behavior of toLocaleString method we have the option to pass two arguments.
More Examples
Let's take more examples to understand the use of the toLocaleString method:
Formating a Number Value into Local Language Formate
In this example, we will convert a given number value into Argentine Spanish language format. To do this we will apply the toLocaleString upon the number by passing the local which is the local language name "es-AR" as a string like this-
Output-
In the example above, we call the toLocaleString upon the myVal variable by passing the local language name value as an argument and assigning the return value to the result variable. When we logged the result variable, as a result, it prints 30.000 on the console.
Formating a Number Value into a Custom Currency Format
In this example, we will convert a given number into a custom currency format. To do this we will apply the toLocaleString upon a given number by passing locals and options as arguments like this-
Output-
In the above example, we called the toLocaleString on a myVal variable by passing the custom style and currency names as a second argument. Then assign the return value in the result variable, when we logged the return it prints "US$ 30.000,00" on the console.
Supported Browsers
| Browser | Chrome | Firefox | Opera | Safari | Edge |
|---|---|---|---|---|---|
| Support | Yes | Yes | Yes | Yes | Yes |
Conclusion
- The toLocaleString is a predefined method in the Number Object in javascript.
- It helps to convert a number value into a local language format.
- We can customize the behavior of the toLocaleString method by passing the custom value of style and currency as arguments.