Commas are commonly used in big numbers to enhance their readability, facilitate quick comprehension, and reduce the likelihood of errors when dealing with numerical data. By visually separating groups of digits, commas provide a clear visual representation of the magnitude of the number, making it easier to read and understand at a glance.

There are several methods to number format with commas in JavaScript but the right way is to use the language method provided for that purpose, which is the toLocaleString() method.

Example

The best way to number format is to use the  toLocaleString() method that returns a string with locale specific representation of the number. This method uses the Intl.NumberFormat API underneath. 

let bigNumber = 529381293;

console.log(bigNumber.toLocaleString('en-us'));
// Expected output 529,381,293

console.log((new Intl.NumberFormat('en-us').format(bigNumber)));
// Expected output 529,381,293 - produces the same output

console.log(bigNumber.toLocaleString('de-DE'));
// Expected output 529.381.293 - In Germany dots are used to separate big numbers

Example with options

The method toLocaleString() also provides a lot of options to customize the output. You can find all of the options here, but in this article, we'll cover only the most popular ones.

Number format with currency symbol

let number = 5828132;

console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD' }));

// Expected output $5,828,132.00

console.log(number.toLocaleString('en-US', { style: 'currency', currency: 'USD', currencyDisplay: 'code' }));

// Expected output USD 5,828,132.00

Number format rounding options

There are also rounding options provided by the method.

let number = 12.532;

console.log(number.toLocaleString('en-US', { maximumFractionDigits: 2, roundingMode: 'floor' }));
// Expected output 12.53

console.log(number.toLocaleString('en-US', { maximumFractionDigits: 2, roundingMode: 'ceil' }));
// Expected output 12.54

console.log(number.toLocaleString('en-US', { maximumFractionDigits: 2, roundingMode: 'ceil', roundingIncrement: 5 }));
// Expected output 12.54

console.log(number.toLocaleString('en-US', { maximumFractionDigits: 2, minimumFractionDigits: 2, roundingMode: 'ceil', roundingIncrement: 10 }));
// Expected output 12.60

Number format with different numbering system

You could for example specify the numbering system as well. For example, in Bali (Indonesia) they count in Balinese, which looks like this.

let number = 5828132;

console.log(number.toLocaleString('id', { numberingSystem: 'bali' }));
// Expected output ᭕,᭘᭒᭘,᭑᭓᭒