Bug #79355 [Opn->Nab]: number round + jscon_decode generate mistery numbers
| From: | requinix@php.net | Date: | Sun, 08 Mar 2020 23:26:00 +0000 |
| Subject: | Bug #79355 [Opn->Nab]: number round + jscon_decode generate mistery numbers | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-225956@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=79355&edit=1
ID: 79355
Updated by: requinix@php.net
Reported by: php at cylancer dot net
Summary: number round + jscon_decode generate mistery numbers
-Status: Open
+Status: Not a bug
Type: Bug
Package: *Math Functions
Operating System: Debian Buster 64Bit
PHP Version: 7.3.15
Block user comment: N
Private report: N
New Comment:
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.
Previous Comments:
------------------------------------------------------------------------
[2020-03-08 23:21:06] 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.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=79355&edit=1