All TalkersCode Topics

Follow TalkersCode On Social Media - A Social Media Network for developers Join Now ➔

Formatting Numbers With Commas And Decimal In JavaScript

Last Updated : Mar 11, 2024

Formatting Numbers With Commas And Decimal In JavaScript

In this article we will show you the solution of formatting numbers with commas and decimal in JavaScript, we will use the toLocaleString() method and also a custom function to format the number.

JavaScript provides a built-in method called'toLocaleString()', which can format numbers with commas and decimals. locale and options are the two arguments, which are.

In this locale argument is optional and formatting the number specifies the locale to use.

The options argument is also optional and provides various formatting options.

Another approach to format the number is to create a custom function. As an argument, the function takes a number and modifies it according to our requirements.

Step By Step Guide On Formatting Numbers With Commas And Decimal In JavaScript :-

// Using toLocaleString() method
const number = 123456789.123456789;
console.log(number.toLocaleString()); // "123,456,789.123"
// Custom function
function formatNumber(num) {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ",");
console.log(formatNumber(number)); // "123,456,789.123"
  1. We first declare a number variable and assign a value to it. The value is a floating-point number with some decimal points.
  2. We then use the toLocaleString() method to format the number. Based on the current locale settings, the method returns a string with commas and decimals. In this case, we did not pass any locale settings, so the default locale is used.
  3. We then create a custom function called formatNumber() that takes a number as an argument.
  4. Inside the function, we first convert the number to a string using the toString() method.
  5. We then use a regular expression to replace every third digit from the right with a comma. The regular expression matches every set of three digits that are not followed by another digit.
  6. Finally, we return the formatted string.
  7. We call the custom function with the number variable as an argument and log the result to the console.

Conclusion :-

Formatting numbers with commas and decimals is a common task in JavaScript.

There are various ways to achieve this, such as using the built-in toLocaleString() method or creating a custom function.

The toLocaleString() method provides a simple and convenient way to format numbers based on the current locale settings.

It takes care of adding commas and decimals automatically, and we can also specify custom formatting options.

On the other hand, creating a custom function gives us more control over the formatting process.

We can define our own rules for adding commas and decimals and create a more customized output.

The ability to format numbers with commas and decimals is an essential skill for any JavaScript developer, and it can greatly improve the readability and usability of our code.

I hope this article on formatting numbers with commas and decimal in JavaScript helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪