Skip to content

Latest commit

 

History

History
17 lines (15 loc) · 462 Bytes

format-number-with-commas.md

File metadata and controls

17 lines (15 loc) · 462 Bytes
title description author tags
Format Number with Commas
Formats a number with commas for better readability (e.g., 1000 -> 1,000).
axorax
number,format
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'