Question
How to Format Numbers as Currency Strings in JavaScript
Question
I want to format a price in JavaScript. I need a function that takes a floating-point number as an argument and returns a string like this:
"$ 2,500.00"
How can I do this?
Short Answer
By the end of this page, you will understand how to turn JavaScript numbers into currency strings, including adding a currency symbol, thousands separators, and fixed decimal places. You will also learn why Intl.NumberFormat is usually the best modern solution, and when simpler approaches like toFixed() may still be useful.
Concept
Formatting a number as currency means converting a raw numeric value such as 2500 or 2500.5 into a human-friendly string such as $2,500.00.
In JavaScript, this usually involves three separate formatting tasks:
- adding a currency symbol like
$ - showing the correct number of decimal places, usually 2 for many currencies
- inserting thousands separators like commas
This matters because raw numbers are fine for calculations, but not ideal for display. Users expect prices, totals, invoices, and reports to be clearly formatted.
There are two common approaches in JavaScript:
- Manual formatting using methods like
toFixed()and string manipulation - Built-in international formatting using
Intl.NumberFormat
For real applications, Intl.NumberFormat is usually the best choice because:
- it handles commas and decimals correctly for different locales
- it supports many currencies
- it avoids a lot of custom string logic
- it is easier to maintain
For example, the same number may be displayed differently depending on region:
- US:
$2,500.00 - Germany:
2.500,00 € - Japan:
¥2,500
So the main idea is: store values as numbers, but format them as strings only when displaying them to users.
Mental Model
Think of a number as the raw value inside a calculator, and currency formatting as the label you attach when presenting the result to a person.
The calculator only cares about the value:
2500
But a user wants a nicely printed price tag:
$2,500.00
So formatting is like packaging a plain item before putting it on a store shelf. The value stays numeric internally, but the displayed version becomes easier to read.
Syntax and Examples
A simple modern solution uses Intl.NumberFormat.
function formatCurrency(amount) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
}
console.log(formatCurrency(2500));
console.log(formatCurrency(2500.5));
console.log(formatCurrency(2500.99));
Output:
$2,500.00
$2,500.50
$2,500.99
How it works
en-UStells JavaScript to use US-style number formattingstyle: "currency"means the output should be formatted as money- selects US dollars
Step by Step Execution
Consider this example:
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
const result = formatter.format(2500.5);
console.log(result);
Step by step:
- JavaScript creates a formatter object with US number rules.
style: "currency"tells it to format values as money.currency: "USD"tells it to use the US dollar format.formatter.format(2500.5)converts the number2500.5into a formatted string.- The result becomes:
"$2,500.50"
Trace with manual formatting
const amount = 2500.5;
const fixed = amount.toFixed(2);
const withCommas = fixed.(, );
result = + withCommas;
.(result);
Real World Use Cases
Currency formatting is used in many everyday programming tasks:
- E-commerce: showing product prices, cart totals, discounts, and taxes
- Finance dashboards: displaying account balances, spending summaries, and revenue
- Invoices and receipts: formatting totals for printable or downloadable documents
- Admin panels: showing order values, payouts, refunds, and reports
- APIs and frontends: keeping numbers raw in data, then formatting them only in the UI
Example in a shopping cart:
const subtotal = 2499.9;
const tax = 125.45;
const total = subtotal + tax;
const usd = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
console.log("Subtotal:", usd.format(subtotal));
console.log("Tax:", usd.format(tax));
console.log("Total:", usd.format(total));
Real Codebase Usage
In real projects, developers usually avoid manual currency string building unless they have a very specific formatting requirement.
Common patterns include:
Reusing one formatter
Creating a formatter once is better than rebuilding it repeatedly.
const usdFormatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
function formatPrice(value) {
return usdFormatter.format(value);
}
Guard clauses for invalid input
function formatPrice(value) {
if (typeof value !== "number" || Number.isNaN(value)) {
return "$0.00";
}
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).(value);
}
Common Mistakes
Here are common beginner mistakes when formatting currency in JavaScript.
1. Using strings instead of numbers
Broken example:
const price = "2500.5";
console.log(price.toFixed(2));
This fails because toFixed() is a number method.
Fix:
const price = Number("2500.5");
console.log(price.toFixed(2));
2. Formatting too early
Broken example:
const price = "$2,500.00";
const total = price + 100;
console.log(total);
This creates string concatenation, not math.
Fix:
const price = 2500;
total = price + ;
.(total);
Comparisons
| Approach | Best for | Pros | Cons |
|---|---|---|---|
Intl.NumberFormat | Real applications | Locale-aware, supports currencies, readable | Slightly more setup |
toFixed() only | Simple decimal formatting | Easy to use | No commas, no currency rules |
toFixed() + regex | Quick custom format | Works for simple fixed output | Manual, less flexible, locale-unaware |
toFixed() vs Intl.NumberFormat
const amount = 2500.5;
console.log(amount.toFixed());
.( .(, {
: ,
:
}).(amount));
Cheat Sheet
// Best modern approach
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
});
formatter.format(2500); // "$2,500.00"
formatter.format(99.5); // "$99.50"
formatter.format(-12.75); // "-$12.75"
// Reusable function
function formatCurrency(amount) {
return new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(amount);
}
// Simple manual approach
function formatCurrency(amount) {
return "$ " + amount.().(, );
}
FAQ
How do I format a number as dollars in JavaScript?
Use Intl.NumberFormat:
new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD"
}).format(2500);
What is the best way to format currency in JavaScript?
For most applications, Intl.NumberFormat is the best choice because it is built in, reliable, and locale-aware.
Can I format currency with commas and two decimals manually?
Yes. You can use toFixed(2) and a regex to add commas, but this is less flexible than Intl.NumberFormat.
Why does toFixed() not add commas?
toFixed() only controls decimal places. It does not apply thousands separators or currency symbols.
Does Intl.NumberFormat work for other currencies?
Yes. You can change the locale and currency code.
.(, {
: ,
:
}).();
Mini Project
Description
Build a small receipt formatter in JavaScript that takes a list of product prices and prints a clean invoice summary. This demonstrates real-world currency formatting because most apps need to show individual prices, subtotals, and totals clearly.
Goal
Create a reusable receipt function that formats each price in US dollars and shows the total.
Requirements
- Create an array of product prices as numbers.
- Format each price as a US currency string.
- Calculate the total using numeric values.
- Display both the item prices and the formatted total.
Keep learning
Related questions
Adding Table Rows with jQuery: append(), Limits, and Best Practices
Learn how to add table rows in jQuery using append(), what elements are allowed in tables, and safer ways to build rows dynamically.
Bower vs npm: What’s the Difference in JavaScript Package Management?
Learn the plain difference between Bower and npm, how each manages packages, and why npm replaced Bower in most JavaScript projects.
Can `(a == 1 && a == 2 && a == 3)` Ever Be True in JavaScript?
Learn how JavaScript type coercion, loose equality, and custom object conversion can make `a == 1 && a == 2 && a == 3` true.