Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

Why can a decimal-looking number change when serialized and read back?

Quick answer

Serialization turns a value into text; parsing that text creates a value according to the receiving system’s numeric type. In JavaScript, a number’s original spelling is not retained: trailing zeros can disappear, and sufficiently large integers can be rounded when parsed. If spelling matters, preserve the original text. If the numeric value matters, check whether every system can represent it with enough precision. [S1][S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

I send a decimal-looking value through JSON, then parse it and serialize it again. Why does the text sometimes come back different—is the number changing?

Geez

There are two things to compare: the number’s meaning and the exact characters used to write it. JSON has a number grammar, but the receiving system’s numeric type determines how precisely a parsed number can be represented. [S1] Once JavaScript has parsed a JSON number into a Number, its later text representation comes from that numeric value, not from a saved copy of the input spelling. [S2]

For example, the text 1.2300 may be represented and emitted as 1.23. Those spellings describe the same ordinary decimal quantity, but they are not identical text. If you need the original number of decimal places for display, a report, or another format-sensitive purpose, keep the original text separately or transmit the value as a string.

Follow-up question

So does a changed-looking value always mean precision was lost? What should I check with a small decimal like 1.2300?

Geez

No. First check whether the numeric value changed or only its spelling. In JavaScript, converting a Number to text does not restore trailing zeros that were present in the input. [S2] A useful comparison is:

  1. Original text: 1.2300
  2. Parsed value: the numeric value represented by the receiving system
  3. Re-serialized text: for example, 1.23

Here the visible difference is formatting, not evidence by itself that the intended quantity changed. Also check whether the application treats the number of decimal places as meaningful. If it does—for example, because the text is a fixed-scale record—do not rely on a numeric value alone to preserve that spelling. Store the required formatted text explicitly.

Follow-up question

What about a large integer that I put in JSON as a number? Can I trust parsing and serializing it to preserve every digit?

Geez

Not automatically. MDN’s JavaScript JSON reference gives an example where parsing 12345678901234567890 as a JavaScript Number yields the nearby value 12345678901234568000; the receiving numeric type cannot represent that input precisely. [S1] Serializing afterward cannot recover digits that were already lost during parsing.

If every digit must survive, represent the value as a string in the data format and keep it a string throughout the path. Turning that string into a Number can lose precision, so avoid that conversion when the recipient cannot preserve the required value. [S1] This example describes JavaScript behavior; it does not establish how every language, parser, or serializer handles numbers.

Follow-up question

Can you give me a practical comparison checklist for testing a value before and after a JSON round trip?

Geez

Use this comparison, and record both the text and value whenever both matter:

  1. Input text: save the exact characters sent, including sign, decimal places, and exponent notation.
  2. Expected meaning: write down the intended numeric value and whether trailing zeros or other formatting carry meaning.
  3. After parsing: inspect the parsed value using the actual receiving system and numeric type.
  4. After serialization: compare the new text with the original, checking both numeric equivalence and exact spelling as separate questions.
  5. Precision requirement: test representative small decimals and large integers; if any digit must be preserved exactly, use a string representation end to end and confirm no step converts it to a number.

For JavaScript, a serialization that changes 1.2300 to 1.23 can reflect formatting, while a sufficiently large integer can already be rounded at parse time. [S1][S2] These checks help identify the difference, but the cited JavaScript references do not specify the behavior of every other system or prove how an untested application handles a value.

A practical checklist

  • Save the exact input text before parsing.

  • Decide whether numeric meaning, original spelling, or both must be preserved.

  • Check the parsed value in the receiving system and note its numeric type.

  • Compare serialized text separately from numeric value; look for changed zeros or notation.

  • Test large integers for exact digits, and keep precision-sensitive values as strings throughout.

A few more useful answers

Why might a number use scientific notation after conversion to text?

In JavaScript, Number.prototype.toString() uses scientific notation for magnitudes at least 10^21 or below 10^-6. That describes JavaScript Number formatting, not every serializer or language. [S2]

If I need the original decimal spelling, can I reconstruct it from the parsed number?

Not reliably in JavaScript: a Number’s text conversion represents its numeric value, not the original spelling, so details such as trailing zeros are not preserved. Keep the original text if those details matter. [S2]

Does this explain numeric behavior in every programming language?

No. The cited behavior concerns JavaScript JSON parsing and Number formatting. Other receiving types and serializers may behave differently, so check the actual systems in your data path. [S1][S2]

Sources

References used for this guide.

  1. [S1] JSON — JavaScript | MDN
    developer.mozilla.org · Checked
  2. [S2] Number.prototype.toString() — JavaScript | MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work