Welcome Guest, Not a member yet? Register   Sign In
Flashdata is messing up numbers
#1

[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;
#2

[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
#3

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




Theme © iAndrew 2016 - Forum software by © MyBB