JavaScript toLocaleString()
The toLocaleString() method converts a value to a string, using local formatting rules.
The toLocaleString() method converts a value to a string, using local formatting rules.
The toLocaleString() method is especially useful for formatting numbers, dates, arrays, currencies, and percentages for different countries.
Share
The toLocaleString() Method
The toLocaleString() method is available for many JavaScript datatypes / objects:
- Numbers
- Dates
- Arrays
- BigInts
Example
Example Code
javascriptlet num = 1234567.89;
let text = num.toLocaleString();Using Locales
You can specify a language and country code to format a value for a specific locale.
Example
Example Code
javascriptlet num = 1234567.89;
let us = num.toLocaleString("en-US");
let de = num.toLocaleString("de-DE");
let no = num.toLocaleString("no-NO");Formatting Currency
With options, toLocaleString() can format numbers as currency.
Example
Example Code
javascriptlet price = 1299.95;
let dollars = price.toLocaleString("en-US",
{style:"currency", currency:"USD"});
let euros = price.toLocaleString("de-DE",
{style:"currency", currency:"EUR"});
let kroner = price.toLocaleString("no-NO",
{style:"currency", currency:"NOK"});Formatting Percentages
The style:"percent" option formats a number as a percentage.
Example
Example Code
javascriptlet score = 0.875;
let result = score.toLocaleString("en-US", {style:"percent"});Controlling Decimal Digits
You can control the number of decimal digits with minimumFractionDigits and maximumFractionDigits.
Example
Example Code
javascriptlet num = 3.14159;
let text = num.toLocaleString("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});Formatting Dates
Dates can also be formatted with toLocaleString().
Example
Example Code
javascriptlet date = new Date();
let text = date.toLocaleString("en-US");Date Formatting Options
Many options can be used to control how dates are displayed.
Example
Example Code
javascriptlet date = new Date();
let text = date.toLocaleString("en-US", {
weekday: "long",
year: "numeric",
month: "long",
day: "numeric"
});A Clever Use: Readable File Sizes
A clever use of toLocaleString() is to make file sizes easier to read.
Example
```javascript function fileSize(bytes) {
if (bytes
Interactive Code Examples
JS Example 1
javascriptlet num = 1234567.89;
let text = num.toLocaleString();JS Example 2
javascriptlet num = 1234567.89;
let us = num.toLocaleString("en-US");
let de = num.toLocaleString("de-DE");
let no = num.toLocaleString("no-NO");JS Example 3
javascriptlet price = 1299.95;
let dollars = price.toLocaleString("en-US",
{style:"currency", currency:"USD"});
let euros = price.toLocaleString("de-DE",
{style:"currency", currency:"EUR"});
let kroner = price.toLocaleString("no-NO",
{style:"currency", currency:"NOK"});JS Example 4
javascriptlet score = 0.875;
let result = score.toLocaleString("en-US", {style:"percent"});JS Example 5
javascriptlet num = 3.14159;
let text = num.toLocaleString("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});JS Example 6
javascriptlet date = new Date();
let text = date.toLocaleString("en-US");