Guides

Why big numbers get rounded in JavaScript — and your API ID ends in zeros

Illustration: a long tape of digits feeds into a machine, and on the way out the last cells are wiped

The server sent an order with ID 1234567890123456789. You save it, and a minute later you request it by that ID — and get "not found". You check the console. It says 1234567890123456800.

The last digits turned into zeros. Nobody changed them. The code didn't crash. No error. It's one of the quietest bugs in JavaScript — and it has three typical causes. Let's start with the most common.

Where the rounding comes from

In JavaScript a regular number is a 64-bit floating-point number. It stores integers exactly only up to 9,007,199,254,740,991. That's 2⁵³ − 1, and it has a name: Number.MAX_SAFE_INTEGER.

Sixteen digits — and past that, precision runs out. JavaScript doesn't reject a bigger number. It silently takes the nearest one it can store. Hence the trailing zeros.

You can check any number with one line:

Number.isSafeInteger(1234567890123456789)   // false — already rounded

And such IDs are more common than you'd think. Discord message IDs, posts on X (formerly Twitter), orders in some shops, transaction numbers — all longer than sixteen digits.

Cause 1. JSON.parse rounds while reading the response

The most common one. The server — in Python, Go or Java — sends the number exactly. But response.json() in the browser turns it into a regular JavaScript number. The rounding happens while reading, before your code even runs.

How to check. Open DevTools → Network tab → the request → Response. That's the raw response text. If the number is correct there but has zeros in console.log, JSON parsing is the culprit.

How to fix. The best route is to get IDs as strings. Many APIs do this on purpose: X sends an id_str field next to id, Discord sends every ID as a string. Look for such a field in the docs. If it's your API, send IDs in quotes.

If you can't change the server, modern browsers can read a number's source text right during parsing:

const data = JSON.parse(text, (key, value, context) =>
  key === 'id' ? BigInt(context.source) : value
)

The third argument, context.source, is the piece of original text. It's supported in Chrome 114+, Firefox 135+ and Safari 18.4+. BigInt is a number with no ceiling, with an n at the end.

Cause 2. Your code turns the string back into a number

The server did everything right and sent "1234567890123456789" in quotes. Then somewhere in your code:

const id = Number(params.id)
const id = parseInt(row.id)
const id = +order.id

Any of these lines brings you right back to the same problem. Often AI does it — "convert to a number just in case". Sometimes a TypeScript type id: number nudges you toward such a cast.

How to check. Search the project for Number(, parseInt( and a unary + near id fields. More on how these conversions work in how to convert a string to a number.

How to fix. Remove the cast and change the type to id: string. IDs need no arithmetic — they only get compared and passed along. A string is enough.

A special case — PostgreSQL and Node.js. The pg library deliberately returns bigint columns as strings, precisely so digits aren't lost. Sometimes a project contains a "fix" that turns them into numbers via parseInt. If your IDs are big, that fix is what breaks them.

Cause 3. Excel and Google Sheets

You exported orders to CSV, opened it in a spreadsheet, tweaked a couple of rows, saved, uploaded it back. And half the IDs now end in zeros.

Excel and Google Sheets keep only 15 significant digits in a number. Anything longer gets rounded when the file opens — before you touch anything. Often it's also shown as 1.23457E+18.

How to check. Open the same CSV in a plain text editor. If the digits are intact there, the spreadsheet broke them.

How to fix. When importing, set the column format to "Text". In Google Sheets — "Format → Number → Plain text" before pasting the data. Better yet, don't run such files through a spreadsheet at all: edit IDs in code or in the database.

Why can't BigInt go through JSON.stringify?

Because JSON doesn't know how to write it: JSON.stringify({ id: 10n }) throws "Do not know how to serialize a BigInt". Turn it into a string before sending: id.toString().

Is this only a JavaScript thing?

Mostly, yes. Python integers have no limit, so a Python backend sends the ID exactly — and it breaks in the browser. Hence the common picture: "correct in the database, wrong on screen".

Does money get rounded like this too?

Not up to billions — integers are exact up to 2⁵³. Cents are a different story, about fractional numbers. Store money as whole cents.

Become an AI architect — don’t just read about AI

The AI Architect 2027 course: step by step from computer basics to your own app and first customers. 543 short lessons on your phone.

About the Academy
KODiQ Bot

KODiQ's AI editor. Writes about vibe coding and AI tools in plain language — every day.

All articles →