CodeIgniter Forums
Flashdata is messing up numbers - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Flashdata is messing up numbers (/thread-12108.html)



Flashdata is messing up numbers - El Forum - 10-06-2008

[eluser][email protected][/eluser]
Hello,

I am storing a decimal number in the flash_data (ie. $42.99) and the serialize function inside the set_flash_data() is appending too many decimals. Later, when I retrieve the value there are issues with comparing the values. Has anyone come across this problem?

Code:
<?php
$val = 42.99;
var_dump($val);  // initial value: float(0.333333333333)
echo '<br/><br/>';
$val = serialize($val);
var_dump($val);  // standart float format: string(55) "d:0.333333333333333303727386009995825588703155517578125;"
echo '<br/><br/>';
$val = preg_replace('/d:([0-9]+(\.[0-9]+)?([Ee][+-]?[0-9]+)?);/e', "'d:'.((float)$1).';'", $val);
var_dump($val);  // compact float format: string(10) "d:0.333333333333;"
echo '<br/><br/>';
$val = unserialize($val);
var_dump($val);  // unserialization successed: float(0.333333333333)
echo '<br/><br/>';
$val = serialize($val);
var_dump($val);  // but value is rounded off!: string(55) "d:0.3333333333330000147753935380023904144763946533203125;"
echo '<br/><br/>';
?&gt;



Flashdata is messing up numbers - El Forum - 10-07-2008

[eluser]Dready[/eluser]
Hello,

the fastest trick I'm thinking of is to store your numbers as strings , something like :
Code:
$val = "42.99";
// then store $val as a flashdata
or
Code:
$val = 42.99;
$val = (string)$val;
// then store $val as a flashdata



Flashdata is messing up numbers - El Forum - 10-08-2008

[eluser][email protected][/eluser]
Thank you Dready, that did the trick.