Thanks to visit codestin.com
Credit goes to coreui.io

How to format a number as currency in JavaScript

Formatting numbers as currency is essential for e-commerce applications, financial displays, pricing components, and implementing features like shopping carts or payment interfaces in JavaScript applications. With over 25 years of experience in software development and as the creator of CoreUI, I’ve implemented currency formatting extensively in components like price displays, checkout forms, and financial dashboards where proper monetary representation is crucial for user trust and international compatibility. From my extensive expertise, the most robust and internationalization-friendly approach is using Intl.NumberFormat with currency options. This method handles locale-specific formatting, currency symbols, and decimal precision automatically while supporting multiple currencies and regions.

Use Intl.NumberFormat with currency style to format numbers as currency.

const price = 1234.56
const formatted = new Intl.NumberFormat('en-US', {
  style: 'currency',
  currency: 'USD'
}).format(price)
// Result: '$1,234.56'

The Intl.NumberFormat constructor creates a number formatter with locale-specific rules. The first parameter 'en-US' specifies the locale, and the options object defines style: 'currency' for monetary formatting and currency: 'USD' for the currency code. The format() method applies the formatting rules to the number. This example produces '$1,234.56' with the dollar symbol, thousands separator, and two decimal places. Different locales and currencies will format differently: 'de-DE' with 'EUR' would produce '1.234,56 €'.

Best Practice Note:

This is the same approach we use in CoreUI components for displaying prices, financial data, and monetary values across our international component library. For simple cases without locale requirements, use toFixed(2) with manual symbol addition. The Intl.NumberFormat approach is preferred for production applications as it handles various currencies, locales, and formatting rules automatically. Consider caching formatter instances for performance when formatting many values.


Speed up your responsive apps and websites with fully-featured, ready-to-use open-source admin panel templates—free to use and built for efficiency.


About the Author

Subscribe to our newsletter
Get early information about new products, product updates and blog posts.
CSS Selector for Parent Element
CSS Selector for Parent Element

How to concatenate a strings in JavaScript?
How to concatenate a strings in JavaScript?

How to loop through an array in JavaScript
How to loop through an array in JavaScript

How to Detect a Click Outside of a React Component
How to Detect a Click Outside of a React Component

Answers by CoreUI Core Team