Format Numbers as Currency with Javascript Format Numbers as Currency with Javascript

If you're using any sort of Javascript framework to dynamically create content, e.g. Loop a JavaScript array to build a table of data, at some point along the way you will have a value that needs to be formatted to a specific currency. To accomplish this I will leverage Javascript's Intl.NumberFormat.

Let's take a look at some code that will take a number and format it to a specific currency's ISO format using Javascript:

var number = 1233445.5678;
var formatter = new Intl.NumberFormat('en-US', { style: 'currency', currency: 'USD' });

The nice part about this function is that if you have ISO Codes you can instantiate the Intl.NumberFormat with your desired locale.

Now, to take this one step further to format numbers as currency using Javascript let's extend the Number type and make it an extension method as follows:

Number.prototype.currency = function(locale, isoCode) {
var formatter = new Intl.NumberFormat(locale, { style: 'currency', currency: isoCode });
return formatter.format(this);

This allow us to simplify the usage as follows:

console.log(number.currency('en-US', 'USD'));

Published on Jun 12, 2022

Tags: JavaScript

Related Posts

Did you enjoy this article? If you did here are some more articles that I thought you will enjoy as they are very similar to the article that you just finished reading.


Learn how to code in HTML, CSS, JavaScript, Python, Ruby, PHP, Java, C#, SQL, and more.

No matter the programming language you're looking to learn, I've hopefully compiled an incredible set of tutorials for you to learn; whether you are beginner or an expert, there is something for everyone to learn. Each topic I go in-depth and provide many examples throughout. I can't wait for you to dig in and improve your skillset with any of the tutorials below.