DEV Community

Ship Stack
Ship Stack

Posted on Originally published at youtube.com

Why String(1e21) Prints 1e+21 but String(1e20) Prints All 21 Digits

String(1e21) gives "1e+21". String(1e20) gives "100000000000000000000", all 21 digits. Same code path, same type, two different renderings, and both are the same number in different clothes.

console.log(String(1e21));  // 1e+21
console.log(String(1e20));  // 100000000000000000000

console.log(1e21 === 1000000000000000000000); // true
Enter fullscreen mode Exit fullscreen mode

The comparison on the last line is true. 1e+21 is not a different value from the long digit string; it is the same double printed in a shorter form.

Where the switch happens

Number.prototype.toString does not always print digits. The spec picks the shortest string that round-trips to the same double, and then decides how to write it based on where the decimal point would land:

  • If the decimal point would sit at position 21 or lower, you get plain digits. 1e20 is a 1 followed by twenty zeros, so you get all 21 characters.
  • Past that, you get exponential notation. 1e21 is 1 followed by twenty-one zeros, and it prints as 1e+21.

The same rule has a small-number side: 0.000001 prints as 0.000001, and 0.0000001 prints as 1e-7.

console.log(String(0.000001));  // 0.000001
console.log(String(0.0000001)); // 1e-7
Enter fullscreen mode Exit fullscreen mode

Why it matters

The value is correct either way, so this is rarely a math bug. It is a formatting and comparison bug:

  • Ids and hashes. A 21-digit id or a large counter shown in a UI or a log can come out as 1e+21. Anyone copying that string into a lookup will not find the row.
  • String comparison. If you compare String(a) === String(b) you are comparing renderings, not values. Two numbers that are equal can print differently if they cross the threshold, and two numbers that print the same way are equal, but the first case is the one that bites.
  • CSV and JSON exports. JSON.stringify(1e21) writes 1e+21, which is valid JSON and parses back to the same number. A downstream spreadsheet may not read it as a number at all.

What to do instead

If a number is an identifier rather than a quantity, do not let it be a number in the first place. Keep it as a string from the moment it enters the process, or use BigInt when you need arithmetic:

console.log(String(1000000000000000000000n)); // 1000000000000000000000
Enter fullscreen mode Exit fullscreen mode

If it is genuinely a quantity and you need a fixed rendering, format it explicitly rather than relying on the default toString. Note that toFixed does not save you here: at 1e21 and above it returns the exponential form too. Going through BigInt does, because BigInt prints every digit:

console.log(BigInt(1e21).toString()); // 1000000000000000000000
Enter fullscreen mode Exit fullscreen mode

And when you see 1e+21 in a log, read it as a hint: whatever wrote it was working with a double, and the number may be past the point where integers are exact. That is a separate trap, worth its own check.

Tested on Node.js 22. The narration in the video is an AI-generated voice; the output above is from a real run.

30-second version: https://www.youtube.com/shorts/c1_sIFy5VGE

Top comments (0)