Open Playground
JS TutorialLesson #108

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

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
let num = 3.14159;

let text = num.toLocaleString("en-US", {

  minimumFractionDigits: 2,

  maximumFractionDigits: 2

});

Note > The method name is toLocaleString(), not toLocalString(). > 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

Example Code

javascript
Try it Yourself
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

Example Code

javascript
Try it Yourself
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

```javascript function fileSize(bytes) {

if (bytes

Interactive Code Examples

JS Example 1

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let num = 1234567.89;



let text = num.toLocaleString();

JS Example 2

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let num = 1234567.89;



let us = num.toLocaleString("en-US");

let de = num.toLocaleString("de-DE");

let no = num.toLocaleString("no-NO");

JS Example 3

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
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"});

JS Example 4

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let score = 0.875;



let result = score.toLocaleString("en-US", {style:"percent"});

JS Example 5

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let num = 3.14159;



let text = num.toLocaleString("en-US", {

  minimumFractionDigits: 2,

  maximumFractionDigits: 2

});

JS Example 6

javascript
Try it Yourself
JavaScript syntax and algorithm demonstration.
let date = new Date();



let text = date.toLocaleString("en-US");