Number.prototype.toLocaleString() in JavaScript

Learn via video course
FREE
View all courses
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
JavaScript Course With Certification: Unlocking the Power of JavaScript
JavaScript Course With Certification: Unlocking the Power of JavaScript
by Mrinal Bhattacharya
1000
4.8
Start Learning
Topics Covered

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 valueLocal language name
ar-SAArabic (Saudi Arabia)
bn-BDBangla (Bangladesh)
bn-INBangla (India)
cs-CZCzech (Czech Republic)
da-DKDanish (Denmark)
de-ATAustrian German
de-CH"Swiss" German
de-DEStandard German (as spoken in Germany)
el-GRModern Greek
en-AUAustralian English
en-CACanadian English
en-GBBritish English
en-IEIrish English
en-INIndian English
en-NZNew Zealand English
en-USUS English
en-ZAEnglish (South Africa)
es-ARArgentine Spanish
es-CLChilean Spanish
es-COColombian Spanish
es-ESCastilian Spanish (as spoken in Central-Northern Spain)
es-MXMexican Spanish
es-USAmerican Spanish
fi-FIFinnish (Finland)
fr-BEBelgian French
fr-CACanadian French
fr-CH"Swiss" French
fr-FRStandard French (especially in France)
he-ILHebrew (Israel)
hi-INHindi (India)
hu-HUHungarian (Hungary)
id-IDIndonesian (Indonesia)
it-CH"Swiss" Italian
it-ITStandard Italian (as spoken in Italy)
ja-JPJapanese (Japan)
ko-KRKorean (Republic of Korea)
nl-BEBelgian Dutch
nl-NLStandard Dutch (as spoken in TheNetherlands)
no-NONorwegian (Norway)
pl-PLPolish (Poland)
pt-BRBrazilian Portuguese
pt-PTEuropean Portuguese (as written and spokenin Portugal)
ro-RORomanian (Romania)
ru-RURussian (Russian Federation)
sk-SKSlovak (Slovakia)
sv-SESwedish (Sweden)
ta-INIndian Tamil
ta-LKSri Lankan Tamil
th-THThai (Thailand)
tr-TRTurkish (Turkey)
zh-CNMainland China, simplified characters
zh-HKHong Kong, traditional characters
zh-TWTaiwan, 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:

KEYVALUE
currencyAny 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.
maximumFractionDigitsTakes a number from 0 to 20, the default value is 3.
maximumSignificantDigitsTakes a number from 1 to 21, default is 21.
minimumFractionDigitsTakes a number from 0 to 20 default is 3.
minimumIntegerDigitsTake a number from 1 to 21 default is 1.
minimumSignificantDigitsTakes a number from 1 to 21 default is 21.
useGroupingfalse, 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

BrowserChromeFirefoxOperaSafariEdge
SupportYesYesYesYesYes

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.