Example 1 — Group a 22-Digit Number Without Losing Digits
A Number can only keep about 16 significant digits, so formatting a huge Number shows made-up zeros. A BigInt keeps every digit.
const grains = 1234567890123456789012n;
console.log(grains.toString()); // 1234567890123456789012
console.log(grains.toLocaleString("en-US")); // 1,234,567,890,123,456,789,012
console.log(Number(grains).toLocaleString("en-US")); // 1,234,567,890,123,456,800,000 How It Works
- toString() gives raw digits. Without formatting, the 22 digits are hard to read, but they are all correct.
- toLocaleString("en-US") adds commas. The BigInt is grouped in threes and every digit stays exact, right down to the final
012. - The Number version is rounded. Converting to a Number first keeps only about 16 significant digits, so the end turns into
800,000. Format the BigInt directly to avoid that.

