php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #42812 integer value returned by php incorrect
Submitted: 2007-10-01 10:33 UTC Modified: 2007-10-01 10:38 UTC
From: laurent at polenord dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.2.4 OS: linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: laurent at polenord dot com
New email:
PHP Version: OS:

 

 [2007-10-01 10:33 UTC] laurent at polenord dot com
Description:
------------
infact the problem occurs on PHP 5.2.3

I want to simply return a float multiplied by a million, and force it to an integer value to avoid php returns int in weird format (2.9E+06 for example), and it doesn't return the correct value.

This is very strange as it seems to occurs only on the '2.09' value : it's ok with 1.09 and 3.09, 2.19, 2.091 and 2.089, etc...


Reproduce code:
---------------
$cpc = '2.09';

echo $cpc * 1000000; // returns correct value : 2090000
echo "<br/>";
echo (int)($cpc * 1000000); // returns 2089999 !!
echo "<br/>";
echo (int)($cpc * 1000 * 1000); // return correct value.

Expected result:
----------------
2090000
2090000
2090000

Actual result:
--------------
2090000
2089999
2090000

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2007-10-01 10:38 UTC] pajoye@php.net
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://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.

NB: remove the (int) cast to see what actually happens.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Mon Dec 01 22:00:01 2025 UTC