Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

json_encode() prints a float like 0.30000000000000004 or 10.0000000001

Errorjson_encode(0.1 + 0.2) returns 0.30000000000000004

Short answer

Since PHP 7.1, json_encode() writes floats with serialize_precision, whose default of -1 prints the shortest value that round-trips exactly. Long digits come from float arithmetic itself, or from a php.ini that sets serialize_precision to 17. Round the number before encoding, set the ini value to -1, or send money as strings or integers.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the two ini values that control float output, and encode a plain 0.1. If 0.1 comes out as 0.1, the setting is fine and the extra digits come from your arithmetic.

    php -r 'var_dump(ini_get("serialize_precision"), ini_get("precision")); echo json_encode(0.1), PHP_EOL;'
  2. 2Match what you see, then jump to its fix

    1. 1json_encode(0.1) prints 0.10000000000000001php.ini sets serialize_precision to 17Fix
    2. 2ini value is already -1, and the long number appears after a sum, a multiplication or a percentageThe float was calculated, and binary floats cannot hold it exactlyFix
    3. 312.0 is sent as 12 and a strict client expects a decimalA whole-number float loses its .0Fix

What does the error look like?

  • json_encode(0.1 + 0.2) returns 0.30000000000000004
  • json_encode(0.1) returns 0.10000000000000001
  • A price like 19.99 comes back with many extra digits after an upgrade

Cause 1 of 3

php.ini sets serialize_precision to 17

1Check

Before PHP 7.1 the default of serialize_precision was 17, so a php.ini carried over from an older server may still set 17. From 7.1 json_encode() uses serialize_precision instead of precision.

php --ini
php -r 'echo ini_get("serialize_precision"), PHP_EOL;'

2Fix

Set the value to -1 in the php.ini that php --ini reports (and the FPM one if it is a separate file). -1 selects the enhanced algorithm that gives the shortest exact value.

; php.ini
serialize_precision = -1

3Verify

Restart PHP-FPM or Apache, then encode 0.1 again.

sudo systemctl restart php8.3-fpm
php -r 'echo json_encode(0.1);'

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The float was calculated, and binary floats cannot hold it exactly

1Check

Decimal fractions such as 0.1 or 0.7 have no exact base-2 form, so the result of arithmetic carries a tiny error. json_encode() is printing the real stored value.

php -r 'var_dump(0.1 + 0.2); echo json_encode(0.1 + 0.2);'

2Fix

Round to the number of decimals you need just before encoding. For money, keep the amount in minor units as an integer, or as a string, and do the maths with BCMath.

$row['total'] = round($a + $b, 2);
echo json_encode($row);

// exact decimals with BCMath
$total = bcadd('0.1', '0.7', 1); // "0.8"

3Verify

Encode the rounded value; it should print with only the decimals you asked for.

php -r 'echo json_encode(round(0.1 + 0.2, 2));'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A whole-number float loses its .0

1Check

This is not a precision bug. Without a flag, json_encode(12.0) gives 12.

php -r 'echo json_encode(12.0), PHP_EOL, json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION);'

2Fix

Pass JSON_PRESERVE_ZERO_FRACTION when the receiver cares about the difference between 12 and 12.0.

echo json_encode($data, JSON_PRESERVE_ZERO_FRACTION);

3Verify

The output now contains 12.0.

php -r 'var_dump(json_encode(12.0, JSON_PRESERVE_ZERO_FRACTION));'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Do not copy an old php.ini between servers; check serialize_precision after every PHP upgrade or server move.
  • Store money as integer minor units or strings, never as floats.
  • Round at the edge, in the code that builds the response, not in the database layer.
  • Compare floats with a small epsilon, not with ==.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.