php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51599 Serialize problem with float data
Submitted: 2010-04-19 11:41 UTC Modified: 2010-04-19 12:14 UTC
From: sdevilcry at gmail dot com Assigned:
Status: Not a bug Package: Unknown/Other Function
PHP Version: Irrelevant OS: Linux / Unix
Private report: No CVE-ID: None
 [2010-04-19 11:41 UTC] sdevilcry at gmail dot com
Description:
------------
The problem appears when I want to serialize data like this the test script submitted 


Output of var_dump : 

string(201) "a:2:{s:5:"exVAT";a:2:{i:0;d:12.1199999999999992184029906638897955417633056640625;i:1;d:12;}s:6:"incVAT";a:2:{i:0;d:4566768.679999999701976776123046875;i:1;d:34343.33999999999650754034519195556640625;}}"


As you can see, the float data became very long... 
I saw that isn't a bug, but coming from floating data type :

=> http://www.math.byu.edu/~schow/work/IEEEFloatingPoint.htm


Test script:
---------------
// Test float 

function NumericPoint($Data)
{
  return round(floatval(str_replace(',','.', $Data)), 2);
}

$tab = array("12.1222", "12", "4566768.67676767", "34343.34343434343434");

$infos = array("exVAT" => array(0 => NumericPoint($tab[0]),
      1 => NumericPoint($tab[1])),
    "incVAT" => array(0 => NumericPoint($tab[2]),
      1 => NumericPoint($tab[3])));

$SeriaData = serialize($infos);


var_dump($SeriaData);

// End

Expected result:
----------------
I expect to see the good format of my float data

Actually, to patch this, I don't convert my string to a float and I use str_replace on my serialize data to delete the double quote and change 's' by 'd'


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-19 12:14 UTC] aharvey@php.net
-Status: Open +Status: Bogus
 [2010-04-19 12:14 UTC] aharvey@php.net
The output of serialize() isn't meant to be human-readable, it's meant to be accurate enough that PHP can unserialise the data later into the exact same values. That level of accuracy is a feature, not a bug, and as you've already noted, it's due to the way IEEE floats are represented.

Not a bug: closing.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat May 04 16:01:31 2024 UTC