session_encode problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
aualtopoll
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2008 12:26 am

session_encode problem

Post by aualtopoll »

Hello
I have a question based on the following code:

session_start();
$n = 50.9096
$_SESSION['number'] = $n;
$file = session_encode();
echo $file;


and the output is: number|d:50.9095999999999975216269376687705516815185546875;

Why is storing that amount of "invented" decimals??

But if i previously assign a format to the number:

session_start();
$n = number_format(50.9096, 4, ".", "");
$_SESSION['number'] = $n;
$file = session_encode();
echo $file;


the output is: number|d:50.9096;

I have the WampServer Version 2.0 on a Windows XP machine.

Thanks in advance.
User avatar
Ziq
Forum Contributor
Posts: 194
Joined: Mon Aug 25, 2008 12:43 am
Location: Russia, Voronezh

Re: session_encode problem

Post by Ziq »

It's well-known trouble this float numbers. If you interested read manual

This php-code output not number|d:50.9096;

Code: Select all

 
<?
session_start();
$n = number_format(50.9096, 4, ".", "");
$_SESSION['number'] = $n;
$file = session_encode();
echo $file;
?>
 
it's return number|s:7:"50.9096";

Same output you may get if use constructions:

Code: Select all

 
$n = "50.9096";
// or
$n = (string)50.9096;
 
aualtopoll
Forum Newbie
Posts: 2
Joined: Thu Sep 11, 2008 12:26 am

Re: session_encode problem

Post by aualtopoll »

thanks!
Post Reply