Tuesday, November 19, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Number toLocaleString() Method

JavaScript Number toLocaleString() Method

The toLocaleString() method converts a number into a string, using a local language format. The language depends on the locale setup on your computer.

Syntax:

number.toLocaleString(locales, options)

Parameters: This method accepts two parameters locales and options.

  • locales: This is an optional parameter. It specifies the language format to be used.
  • options: This parameter is also an optional one.
    • It includes maximumFractionDigits that represent a number from 0 to 20 (default is 3)
    • It includes maximumSignificantDigits that represent a number from 1 to 21 (default is 21).

Note: There are other options as parameters such as minimumFractionDigits, minimumSignificantDigits, currency, and many more. The developer can use them as per the requirement of the application.

Return value: The return value can be a string that represents a number.

Example: The following code demonstrates all the conversions.

Javascript




// Declaring an variable a
let n = new Number(705870689);
console.log("ar-SA: "
            + n.toLocaleString("ar-SA"));
console.log("bn-BD: "
            + n.toLocaleString("bn-BD"));
console.log("bn-IN: "
            + n.toLocaleString("bn-IN"));
console.log("cs-CZ: "
            + n.toLocaleString("cs-CZ"));


Output:

ar-SA: ٧٠٥٬٨٧٠٬٦٨٩
bn-BD: ৭০,৫৮,৭০,৬৮৯
bn-IN: ৭০,৫৮,৭০,৬৮৯
cs-CZ: 705 870 689

Example 2: The following example demonstrates the style and currency attributes.

Javascript




// Declaring an variable a
let a = new Number(159900);
 
// Creating an dictionary like object and
// include currency and style
let myObj = {
    style: "currency",
    currency: "EUR"
}
 
console.log(a.toLocaleString("en-GB", myObj));


Output:

€159,900.00

Supported Browsers:

  • Google Chrome 1 and above
  • Internet Explorer 3 and above
  • Firefox 1 and above
  • Apple Safari 1 and above
  • Opera 4 and above
  • Edge 12 and above

We have a complete list of JavaScript Number constructor, properties, and methods list, to know more about the numbers please go through that article.

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments