|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #79355 number round + jscon_decode generate mistery numbers
Submitted: 2020-03-08 23:21 UTC Modified: 2020-03-09 08:16 UTC
From: php at cylancer dot net Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 7.3.15 OS: Debian Buster 64Bit
Private report: No CVE-ID: None
 [2020-03-08 23:21 UTC] php at cylancer dot net
I use the PHP Version 7.3.14-1~deb10u1

After Json decoding, rounded numbers have many unexpected decimal places. 

Test script:
$a = 4; 
$b = 6;
$c = $a * 100 / $b;
echo '$c = '. $c." \n\n";
$d = round($c, 1); // the second parameter is important
echo '$d = round($c, 1);'."\n\n";
echo '$d = '.$d. ' <--- is rounded $c'."\n\n";
echo 'if i decode my result to json... '."\n\n";
echo json_encode($d)."\n\n";
echo 'then my number is not rounded.'."\n\n";

The output is: 

$c = 66.6666666667 
$d = round($c, 1);
$d = 66.7 <--- is rounded $c
if i decode my result to json... 
then my number is not rounded.


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-08 23:26 UTC]
-Status: Open +Status: Not a bug
 [2020-03-08 23:26 UTC]
Floating point values have a limited precision. Hence a value might
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is, read this:

Thank you for your interest in PHP.

 [2020-03-09 08:16 UTC]
You likely have serialize_precision set to some unnecessarily high value. Please leave serialize_precision at the default of -1, unless you really know what you're doing.
 [2020-03-09 08:32 UTC] php at cylancer dot net
Thank you very much. The "serialize_precision" was really set to an absurdly high value. Unfortunately I did not recognize the connection. 

If possible, I withdraw my bug report.  

Thanks again for the quick clarification and help.

Best regards 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Mar 03 17:01:27 2024 UTC