Loading...

How to Format a Number as Currency in JavaScript

How to Format a Number as Currency in JavaScript

As a developer, you may have come across situations where you need to display numbers as currency, with proper formatting like commas, currency symbols, and decimal points. In this blog post, we'll discuss various methods to format a number as currency in JavaScript, keeping the requirements of codedamn users in mind. We'll cover some traditional methods and also explore the standard library way for formatting numbers.

Introduction to Number Formatting

In JavaScript, numbers can be represented in various formats, such as binary, octal, decimal, and hexadecimal. In addition, we can represent numbers with different notations, like exponential, fixed-point, or as currency. Formatting a number as currency involves adding commas as thousands separators, specifying the number of decimal places, and appending a currency symbol.

Let's dive into different techniques to format numbers as currency in JavaScript.

Using the toLocaleString() Method

The toLocaleString() method is a built-in JavaScript method that converts a number to a string with local formatting. This method takes two optional arguments: locales and options. The locales argument is used to specify the locale, while the options argument is an object that can customize the formatting.

Here's an example:

const number = 123456.789; const formattedNumber = number.toLocaleString('en-US', { style: 'currency', currency: 'USD', }); console.log(formattedNumber); // Output: $123,456.79

In this example, we passed the locale 'en-US' and set the style option to 'currency', which formats the number as currency. The currency option is set to 'USD', which specifies the currency type.

Using Template Literals and Regular Expressions

Another approach to format numbers as currency is by using template literals and regular expressions. This method might be more suitable for situations where you want better control over the formatting.

Here's an example:

const formatCurrency = (number, symbol = '$') => { // Add thousands separator const formattedNumber = number.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ','); // Format the number as a currency string return `${symbol}${formattedNumber}`; }; const number = 123456.789; console.log(formatCurrency(number)); // Output: $123,456.789

In this example, we created a formatCurrency function that takes a number and an optional currency symbol as arguments. The function uses regular expressions to add commas as thousands separators and then returns the formatted currency string using a template literal.

Using the Internationalization API (ECMAScript 402)

ECMAScript 402, also known as the Internationalization API, is a standard that provides a set of objects, methods, and options to support language-sensitive operations. The Intl.NumberFormat object is part of this API, and it allows you to format numbers as strings according to the given locale and formatting options.

Here's an example:

const number = 123456.789; const formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD', }); const formattedNumber = formatter.format(number); console.log(formattedNumber); // Output: $123,456.79

In this example, we created an instance of Intl.NumberFormat by passing the locale 'en-US' and the formatting options object. We set the style option to 'currency' and the currency option to 'USD'. Then, we called the format() method on the formatter object to format the number as currency.

FAQ

Q: Can I format a number as currency with a specific number of decimal places?

A: Yes, you can control the number of decimal places by using the minimumFractionDigits and maximumFractionDigits options in the toLocaleString() method or the Intl.NumberFormat object.

const number = 123456.789; const formattedNumber = number.toLocaleString('en-US', { style: 'currency', currency: 'USD', minimumFractionDigits: 2, maximumFractionDigits: 2, }); console.log(formattedNumber); // Output: $123,456.79

Q: How can I format a number as currency for different locales?

A: You can format a number as currency for different locales by changing the locales argument in the toLocaleString() method or the Intl.NumberFormat object.

const number = 123456.789; const formattedNumber = number.toLocaleString('de-DE', { style: 'currency', currency: 'EUR', }); console.log(formattedNumber); // Output: 123.456,79 €

Q: What is the difference between the toLocaleString() method and the Intl.NumberFormat object?

A: Both the toLocaleString() method and the Intl.NumberFormat object are part of the Internationalization API. The main difference is that the toLocaleString() method is a built-in method of the Number prototype, while the Intl.NumberFormat object is a separate object specifically designed for number formatting.

In conclusion, JavaScript provides various methods to format numbers as currency, such as the toLocaleString() method, regular expressions with template literals, and the Intl.NumberFormat object. Choose the method that best fits your needs and provides the desired level of customization and control over the formatting. Happy coding!

Sharing is caring

Did you like what Mehul Mohan wrote? Thank them for their work by sharing it on social media.

0/10000

No comments so far