There’s a simple, native way to format numbers and floats as currency strings in JavaScript. I’ve seen all kinds of approaches to this common scenario. The toLocaleString method is built-in to JavaScript, 100% supported in majority browsers, and requires no external libraries or dependencies. I’d recommend it as the best approach to take if you want to format a price in JavaScript or TypeScript in one of the following common USD formats.

Display as US Dollars & Cents: $0,000.00

const amount = 2500;

amount.toLocaleString("en-US", {
  style: "currency",
  currency: "USD"
});

// "$2,500.00"

Display as Rounded US Dollars: $0,000

const amount = 2500;

amount.toLocaleString("en-US", {
  style: "currency",
  currency: "USD",
  minimumFractionDigits: 0,
});

// "$2,500"

For more information check out the official toLocaleString documentation, it can be used to display numbers and floats in many other international and localized formats.

About the Author

Kevin Leary is a freelance developer in Boston, MA with a strong portfolio of work, and over 13 years of experience as a programming consultant and digital strategy provider. Interested in working together? Hire me for your next project.