Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
JavaScript 
3.
Number.prototype.toLocaleString()
3.1.
Locales
3.1.1.
Example 1
3.2.
Javascript
3.2.1.
Output
3.2.2.
Example 2
3.3.
Javascript
3.3.1.
Output
3.4.
Options 
3.4.1.
Example 1
3.5.
Javascript
3.5.1.
Output
3.5.2.
Example 2
3.6.
Javascript
3.6.1.
Output
4.
Frequently Asked Questions
4.1.
Are there any browser compatibility issues with toLocaleString()?
4.2.
How can I handle non-numeric input with toLocaleString()?
4.3.
Can I use toLocaleString() for date and time formatting as well?
5.
Conclusion
Last Updated: Mar 27, 2024
Easy

Number.prototype.toLocaleString() in JavaScript

Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In 1995, Netscape announced JavaScript as an “easy-to-use object scripting language designed for creating live online applications that link objects and resources on both clients and servers.” Since then, it has become the de facto standard for client-side scripting in Web browsers, but many other applications also include a JavaScript engine. 

This prevalence has led developers to write large programs in a language conceived for scripting but not for programming in the large. Hence, tool support is badly needed to help debug and maintain these programs.

Number prototype toLocaleString() in JavaScript

This is where Number.prototype.toLocaleString() steps in as a powerful tool for formatting numbers according to locale-specific conventions.  In this article, we will learn about Number.prototype.toLocaleString() in JavaScript.

JavaScript 

JavaScript is an object-based language that uses prototype objects to model inheritance. As virtually all predefined operations are accessed via prototype objects, the analysis must model these objects precisely. 

Objects are mappings from strings (property names) to values. In general, properties can be added and removed during execution, and property names may be dynamically computed. 

JavaScript

Undefined results, such as accessing a non-existing property of an object, are represented by a particular value unclear. Still, there is a subtle distinction between an object that lacks a property and an object with undefined property.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Number.prototype.toLocaleString()

The JavaScript function ‘number.toLocaleString(locales, options)’ is a flexible way to format numbers by locale-specific conventions. Developers can add thousands of separators, specify the number of decimal places, and format numerical values as currency or percentages, among other customizations. The breakdown of its two parameters is as follows:

Locales

A string with a BCP 47 language tag or an array of strings with this tag indicates the locales to use for formatting. The formatting rules, including the thousands separator and decimal separator, are controlled by this parameter. The default locale is applied if this parameter is left blank or set to undefined.

Example 1

  • Javascript

Javascript

const number = 1234567.89;
const formatted = number.toLocaleString('en-US');
console.log(formatted);

Output

Output

Example 2

  • Javascript

Javascript

const number = 1234567.89;
const formatted = number.toLocaleString(['fr-FR', 'en-US']);
console.log(formatted);


Output

Output

Options 

An object with properties defining extra formatting choices. You can modify how the number is displayed by setting one of these options, the number of decimal places, configuring currency symbols, and more.

Example 1

  • Javascript

Javascript

const number = 1234.56;
const formatted = number.toLocaleString('en-US', { style: 'currency', currency: 'USD' });
console.log(formatted);

Output

Output

Example 2

  • Javascript

Javascript

const number = 1234.56789;
const formatted = number.toLocaleString('en-US', { maximumFractionDigits: 2 });
console.log(formatted);


Output

Output

Frequently Asked Questions

Are there any browser compatibility issues with toLocaleString()?

Modern browsers generally support toLocaleString(), but some older browsers may not support it at all or may behave differently. Testing in your target browsers is crucial, and you should consider using polyfills or libraries for consistent formatting.

How can I handle non-numeric input with toLocaleString()?

Number formatting is supported by the toLocaleString() function. Non-numeric input may cause unexpected behavior or errors if you pass it. Make sure the values you pass to this function are real numbers.

Can I use toLocaleString() for date and time formatting as well?

toLocaleString() is primarily used for number formatting. You should use Date.prototype.toLocaleString() or other date and time formatting functions for date and time formatting.

Conclusion

In this article, we learn about Number.prototype.toLocaleString() in JavaScript. We also learn about JavaScript. We concluded the article Number.prototype.toLocaleString() and their examples.

To better understand the topic, refer to 

For more information, refer to our Guided Path on CodeStudio to upskill yourself in PythonData Structures and AlgorithmsCompetitive ProgrammingSystem Design, and many more! 

Head over to our practice platform, CodeStudio, to practice top problems, attempt mock tests, read interview experiences and interview bundles, follow guided paths for placement preparations, and much more!
Happy Learning!

Previous article
JavaScript String indexOf() method
Next article
Math min() JavaScript Function
Live masterclass