php.net |  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: php at cylancer dot net
New email:
PHP Version: OS:

 

 [2020-03-08 23:21 UTC] php at cylancer dot net
Description:
------------
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... 
66.7000000000000028421709430404007434844970703125
then my number is not rounded.



Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-03-08 23:26 UTC] requinix@php.net
-Status: Open +Status: Not a bug
 [2020-03-08 23:26 UTC] requinix@php.net
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:
http://www.floating-point-gui.de/

Thank you for your interest in PHP.


 [2020-03-09 08:16 UTC] nikic@php.net
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 
Clemens
 
PHP Copyright © 2001-2021 The PHP Group
All rights reserved.
Last updated: Sat Jan 23 15:01:23 2021 UTC