DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. JavaScript
  4. How to Format a Number as Currency in JavaScript

How to Format a Number as Currency in JavaScript

This article will walk you through how to format a number as currency in JavaScript.

Dennis Mwangi user avatar by
Dennis Mwangi
·
Feb. 07, 23 · Tutorial
Like (3)
Save
Tweet
Share
3.97K Views

Join the DZone community and get the full member experience.

Join For Free

Every country has its currency and different patterns or ways of displaying monetary amounts. When we appropriately express a number, it is easier to read and comprehend for readers.

When you use data from an API or an external resource, it will be in some generic format. For instance, if you are creating a store, you may have data such as pricing.

This article will walk you through how to format a number as Currency in JavaScript.

Let's dive in!

We will be using a random number, such as 17225, as shown in the arrays below:

JavaScript
 
const Journals = [

    {

        "id": 01,

        "name": "Software Development",

        "price": 100.80,

    },

    {

        "id": 02,

        "name": "Introduction to Programming",

        "price": 1534,

    },

        {

        "id": 04,

        "name": "Program or Be Programmed",

        "price": 17225,

    }

]


Even adding a currency sign does not solve the problem since commas and decimals must be added in the right locations. You would also like each price output to be formatted correctly, dependent on the currency. 

For example, 17225 would be $17,225.00 (US Dollars), 17,225.00 (Rupees), or €17,225,00 (Euros), depending on your chosen currency, location, and style. You may also use JavaScript's Intl.NumberFormat() function to convert these integers to currencies.

JavaScript
 
const price = 17225;

let KenyaShilling = new Intl.NumberFormat('en-Ke',

{

    style: 'currency',

    currency: 'KSH',

});

console.log(`The formatted version of {price} is {KenyaShilling.format(price)}`);

// The formatted version of 17225 is Ksh17,225.00


Output:

 
 Ksh 17,225.00  


How to Format Numbers as Currency Using the Intl.NumberFormat() Constructor

You may build Intl.NumberFormat objects that enable language-sensitive numerical formatting, such as currency formatting, using the Intl.NumberFormat() constructor.

This constructor considers two important factors: locales and options, which are both optional.

 
new Intl.NumberFormat(locales, options) // we can also use Intl.NumberFormat(locales, options)


Remember that Intl.NumberFormat() can be used either with or without "new." Both will create a new Intl.NumberFormat instance.

When no locale or option is given to the Intl.NumberFormat() constructor will simply format the integer by adding commas.

 
const price = 17225; console.log(new Intl.NumberFormat().format(price));


Output:

 
 17,225


As noted above, you are not looking for standard number formatting. Instead, you want to structure these numbers as currency so that it returns the currency sign with suitable formatting rather than having to write it manually.

We can now have a look and explore both parameters.

The First Argument: Locales

The locale argument is an optional string parameter that could be given. It denotes a particular geographical, political, or cultural territory. It only prepares the number according to the location and does not include currency formatting.

 
const price = 172250; console.log(new Intl.NumberFormat('en-US').format(price)); // 172,250 console.log(new Intl.NumberFormat('en-IN').format(price)); // 1,72,250 console.log(new Intl.NumberFormat('en-DE').format(price)); // 172.250


You will see that the numbers and prices have been formatted regionally based on your location. Let's look at the options parameter now to see how we may change the numbers to represent a currency.

The Second Argument: Options (Style, Currency…)

This is the major parameter, and you may use this to apply additional formatting, such as currency formatting. This is a JavaScript object that has additional arguments, such as:

Style: This specifies the sort of formatting you desire. This includes values such as decimals, currencies, and units.

Currency; is an additional choice. You may use this option to indicate the currency to format to, such as USD, CAD, GBP, INR, and many more.

JavaScript
 
// format number to US dollar

let USDollar = new Intl.NumberFormat('en-US', {

    style: 'currency',

    currency: 'USD',

});

// format number to British pounds

let Pounds = Intl.NumberFormat('en-GB', {

    style: 'currency',

    currency: 'GBP',

});

// format number to Indian rupee

let Rupee = new Intl.NumberFormat('en-IN', {

    style: 'currency',

    currency: 'INR',

});

// format number to Euro

let Euro = Intl.NumberFormat('en-DE', {

    style: 'currency',

    currency: 'EUR',

});

 

console.log('Dollars: ' + USDollar.format(price));

// Dollars: $172,250.00

console.log(`Pounds: ${pounds.format(price)}`);

// Pounds: £172,250.00

console.log('Rupees: ' + rupee.format(price));

// Rupees: ₹1,72,250.00 

console.log(`Euro: ${euro.format(price)}`);

// Euro: €172,250.00


The Third Argument: Maximum Significant Digits

MaximumSignificantDigits is another option. This allows you to round the price variables based on the number of significant figures you choose. 

For example, if you change the value to 3, 172,250.00 becomes 172,000.

JavaScript
 
let  euro= Intl.NumberFormat('en-'Euro, {

    style: 'currency',

    currency: 'EUR',

    maximumSignificantDigits: 3,

});

console.log(`Euro: ${euro.format(price)}`);

// Euro: £172,000


The scope of this article is just the basics of how to use JavaScript to convert a random number to a currency format.

Happy coding!

JavaScript Java Data Objects JavaScript library Object type (object-oriented programming)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • A Beginner's Guide to Infrastructure as Code
  • Front-End Troubleshooting Using OpenTelemetry
  • Testing Repository Adapters With Hexagonal Architecture
  • DevOps vs Agile: Which Approach Will Win the Battle for Efficiency?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: