JavaScript Number Format Currency
Last Updated : Mar 11, 2024
IN - JavaScript | Written & Updated By - Anjali
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
- We define a function called formatCurrency that takes a number parameter.
- We convert the number to a string using the toString() method and store it in the strNumber variable.
- 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.
- 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.
- We create the formattedCurrency string by concatenating the dollar sign ('$') with the formattedWholePart.
- If the decimalPart exists, we append it to the formattedCurrency string, preceded by a period ('.').
- 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.