JavaScript Number toLocaleString() Method
The JavaScript Number toLocaleString() method is used to represent a number as a string, using the locale language format. The locale language depends on the locales set up on your computer. It returns a string with a language-sensitive representation of this number.
Note: Locales is a string with a BCP 47 language tag, or an array of such strings.
Following are the lists of some locales for different countries −
- en-IN − Represents the locale for an "Indian" English.
- en-US − Represents the locale for an "US" English.
- ar-EG − Represents the locale for an "Arabic" langauge.
Syntax
Following are the syntax of JavaScript Number toLocaleString() method −
toLocaleString(locales, options)
Parameters
This method accepts two parameters named 'locales' and 'options', where the 'locales' parameter is optional, which are described below −
- locales (optional) − It is a specific language format to use.
- options − It is an object adjusting the output format.
Return value
This method returns string representing the given number according to language-specific format.
Example 1
If no parameter is passed to this method, it returns the number as a string using the default locale language format.
<html>
<head>
<title>JavaScript toLocaleString() method</title>
</head>
<body>
<script>
const number = 2345;
document.write("Given option value = ", number);
document.write("<br>String representation = ", number.toLocaleString());
</script>
</body>
</html>
Output
The above program returns the new string representation as 2, 345 −
Given option value = 2345 String representation = 2,345
Example 2
If we pass 'fi-FI' as the optional 'locale' parameter value, it will format a number into a string using the Finnish language and conventions.
<html>
<head>
<title>JavaScript toLocaleString() method</title>
</head>
<body>
<script>
const number = 120131;
const locale = "fi-FI";
document.write("Given number value = ", number);
document.write("<br>Locale value = ", locale);
document.write("<br>String representation(FINLAND language) = ", number.toLocaleString(locale));
</script>
</body>
</html>
Output
The above program converts the number value into the string using the specific language 'FINLAND'.
Given number value = 120131 Locale value = fi-FI String representation(FINLAND language) = 120 131
Example 3
If we pass 'en-US' as the 'locale' parameter value and 'USD' as the 'option' parameter value, it will format the number as a string using the US English language and currency.
<html>
<head>
<title>JavaScript toLocaleString() method</title>
</head>
<body>
<script>
const number = 1350;
const locale = "en-US";
const option = {style: "currency", currency: "USD"};
document.write("Given number value = ", number);
document.write("<br>Locale value = ", locale);
document.write("<br>Option value = ", option.style, ' : ', option.currency);
document.write("<br>String representation(US) = ", number.toLocaleString(locale, option));
</script>
</body>
</html>
Output
After executing the above program, it will convert the number as string into the US currency format.
Given number value = 1350 Locale value = en-US Option value = currency : USD String representation(US) = $1,350.00