Current Location: Home> Latest Articles> How to Properly Handle Number Precision in JSON When Using the json_decode Function

How to Properly Handle Number Precision in JSON When Using the json_decode Function

gitbox 2025-09-02

1. Why Do Number Precision Issues Occur?

The number type used in JSON is based on JavaScript's Number type, which has certain precision limitations. JavaScript's Number type follows the IEEE 754 double-precision floating-point standard (64-bit), capable of representing up to 15 to 17 significant digits. Numbers in JSON that exceed this range may lose precision during conversion.

In PHP, json_decode by default parses numbers in JSON as floating-point types (float), which also follow the IEEE 754 standard. This type differs slightly in precision compared to JavaScript's Number, especially for extremely large or small numbers.

For example, if a JSON object contains a number like 12345678901234567890, decoding it with json_decode may result in precision loss, producing an inaccurate value.

2. How to Solve Number Precision Issues?

To better control number precision, PHP offers several methods to handle precision issues when using json_decode.

2.1 Using the JSON_BIGINT_AS_STRING Option

If numbers in JSON may exceed the precision range of float (e.g., large integers), you can use the JSON_BIGINT_AS_STRING option when calling json_decode to parse these large numbers as strings instead of floating-point numbers. This prevents precision loss, as strings retain the full numeric value.

<span><span><span class="hljs-variable">$json</span></span><span> = </span><span><span class="hljs-string">&#039;{"big_number": 12345678901234567890}&#039;</span></span><span>;
</span><span><span class="hljs-variable">$data</span></span><span> = </span><span><span class="hljs-title function_ invoke__">json_decode</span></span><span>(</span><span><span class="hljs-variable">$json</span></span>, </span><span><span class="hljs-literal">false</span></span><span>, </span><span><span class="hljs-number">512</span></span>, JSON_BIGINT_AS_STRING);
<p></span>echo $data->big_number;  // Outputs "12345678901234567890"<br>
</span>

In this way, PHP keeps the number 12345678901234567890 as a string rather than converting it to a float, avoiding precision loss.

2.2 Manually Handling Number Precision

Another approach is to control precision manually after receiving JSON data. For example, using the GMP (GNU Multiple Precision) extension to handle large numbers. GMP allows for arbitrary-precision integers and is useful when precise calculations are needed.

<span><span><span class="hljs-variable">$json</span></span><span> = </span><span><span class="hljs-string">&#039;{"big_number": 12345678901234567890}&#039;</span></span><span>;
</span><span><span class="hljs-variable">$data</span></span><span> = </span><span><span class="hljs-title function_ invoke__">json_decode</span></span><span>(</span><span><span class="hljs-variable">$json</span></span>, </span><span><span class="hljs-literal">true</span></span>);
<p></span>$bigNumber = gmp_init($data['big_number']);<br>
echo gmp_strval($bigNumber);  // Outputs "12345678901234567890"<br>
</span>

In this example, the number is parsed as a string, and GMP ensures the precision remains intact.

2.3 Using Appropriate Data Formats

If JSON contains a mix of numbers and strings, it’s best to clearly distinguish data types when processing it. For scenarios with very large or very small numbers, ensure that JSON data is generated using suitable formats. Using scientific notation or string representation for large numbers can reduce the risk of precision loss.

<span><span><span class="hljs-variable">$json</span></span><span> = </span><span><span class="hljs-string">&#039;{"big_number": "12345678901234567890"}&#039;</span></span><span>;
</span><span><span class="hljs-variable">$data</span></span><span> = </span><span><span class="hljs-title function_ invoke__">json_decode</span></span><span>(</span><span><span class="hljs-variable">$json</span></span>, </span><span><span class="hljs-literal">true</span></span>);
<p></span>echo $data['big_number'];  // Outputs "12345678901234567890"<br>
</span>

This approach prevents PHP from converting numbers to floats and preserves data integrity.

3. Other Considerations

3.1 Issues with Floating-Point Comparisons

In PHP, floating-point numbers may lose precision, leading to inaccurate comparisons. To avoid this, use strings or precise numeric types for comparisons, especially in financial calculations or other scenarios requiring high precision.

3.2 Performance Considerations

Using the JSON_BIGINT_AS_STRING option causes PHP to treat all large integers as strings during JSON parsing, which may affect performance, especially with large datasets. Therefore, it’s important to balance precision requirements with performance when choosing a solution.

4. Conclusion

Handling number precision in JSON is a significant challenge, particularly for large numbers or high-precision requirements. PHP provides multiple methods to address this, such as using the JSON_BIGINT_AS_STRING option to parse large numbers as strings or using the GMP extension for precise calculations. Choosing the appropriate method based on your needs ensures data accuracy and system stability.