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.
The toLocaleString() Method
The toLocaleString() method is available for many JavaScript datatypes / objects:
- Numbers
- Dates
- Arrays
- BigInts
Example
let 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
let 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
let 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
let 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
let num = 3.14159;
let text = num.toLocaleString("en-US", {
minimumFractionDigits: 2,
maximumFractionDigits: 2
});
Note: The method name is
toLocaleString(), nottoLocalString(). The word locale means a language and country format, such as"en-US","de-DE", or"no-NO".
Formatting Dates
Dates can also be formatted with toLocaleString().
Example
let date = new Date();
let text = date.toLocaleString("en-US");
Date Formatting Options
Many options can be used to control how dates are displayed.
Example
let 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
function fileSize(bytes) {
if (bytes < 1024) return bytes + " bytes";
if (bytes < 1024 * 1024) return (bytes / 1024).toLocaleString("en-US", {maximumFractionDigits: 1}) + " KB";
return (bytes / 1024 / 1024).toLocaleString("en-US", {maximumFractionDigits: 1}) + " MB";
}
let size = 1536000;
let text = fileSize(size);
Arrays and toLocaleString()
For arrays, the toLocaleString() methods converts each array element.
Example
const dates = [
new Date("2026-01-01"),
new Date("2026-12-24")
];
let text = dates.toLocaleString("en-US");