QuickSnip LogoA light blue logo with a thunderbolt in bracketsQuickSnip

Format Number with Commas

Formats a number with commas for better readability (e.g., 1000 -> 1,000).

  • number
  • format

Install

npx shadcn@latest add https://quicksnip.dev/r/format-number-with-commas.json

This snippet is not verified yet. Review it before use, or help verify it.

Code

const formatNumberWithCommas = (num) => {
  return num.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ',');
};

Usage

formatNumberWithCommas(1000); // Returns: '1,000'
formatNumberWithCommas(1234567); // Returns: '1,234,567'
formatNumberWithCommas(987654321); // Returns: '987,654,321'