All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

JavaScript Number Format Currency

Last Updated : Mar 11, 2024

JavaScript Number Format Currency

In this article we will show you the solution of JavaScript number format currency, in this, we will explore how to format numbers as currency in JavaScript.

Formatting numbers as currency is a common requirement in web development when working with financial applications or displaying monetary values.

We will learn how to convert a number into a formatted currency string using JavaScript.

To format a number as currency in JavaScript, we follow a step-by-step process.

First, we convert the given number to a string using the toString() method. Next, we split the string representation into two parts: the whole part and the decimal part, by using the split() method on the decimal point.

Then, we add thousands separators to the whole part by utilizing a regular expression with the replace() method, inserting commas every three digits.

After that, we create the formatted currency string by concatenating the dollar sign with the formatted whole part.

Finally, if a decimal part exists, we append it to the formatted currency string preceded by a period. This method ensures that the number is properly formatted as currency, ready for display.

Step By Step Guide On JavaScript Number Format Currency :-

// Format a number as currency
function formatCurrency(number) {
  let strNumber = number.toString();
  let parts = strNumber.split('.');
  let wholePart = parts[0];
  let decimalPart = parts[1];
  let formattedWholePart = wholePart.replace(/\B(?=(\d{3})+(?!\d))/g, ',');
  let formattedCurrency = '$' + formattedWholePart;
  if (decimalPart) {
    formattedCurrency += '.' + decimalPart;
  }
  return formattedCurrency;
}
// Example usage
let number = 1234567.89;
let formattedNumber = formatCurrency(number);
console.log(formattedNumber);
// Output: $1,234,567.89
  1. We define a function called formatCurrency that takes a number parameter.
  2. We convert the number to a string using the toString() method and store it in the strNumber variable.
  3. We split the strNumber into two parts, the whole part and the decimal part, using the split() method. We store the whole part in the wholePart variable and the decimal part in the decimalPart variable.
  4. We use a regular expression and the replace() method to add commas to the wholePart every three digits. The resulting formatted whole part is stored in the formattedWholePart variable.
  5. We create the formattedCurrency string by concatenating the dollar sign ('$') with the formattedWholePart.
  6. If the decimalPart exists, we append it to the formattedCurrency string, preceded by a period ('.').
  7. Finally, we return the formattedCurrency string.

Conclusion :-

we learned how to format numbers as currency in JavaScript.

By using the formatCurrency function, we can convert a number into a formatted currency string that includes commas for thousands separation and a dollar sign.

This formatting is useful for displaying monetary values in a user-friendly manner.

I hope this article on JavaScript number format currency helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪