php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #48846 convertin int to float
Submitted: 2009-07-08 06:52 UTC Modified: 2009-07-08 20:35 UTC
From: marcin at datamomentu dot eu Assigned:
Status: Not a bug Package: Variables related
PHP Version: 5.3.0 OS: Windows XP
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: marcin at datamomentu dot eu
New email:
PHP Version: OS:

 

 [2009-07-08 06:52 UTC] marcin at datamomentu dot eu
Description:
------------
$t1 = floatval(8.95);
		var_dump($t1);
		$t2 = $t1*100;
		var_dump($t2);
		$t3 = (int)$t2;
		var_dump($t3);
		
t3 should be 8.95 and its 8.94

its true for t1 = 7.95 or 9.95 

its not true for t1 = 6.95 or 5.95 or 4.95 etc 



Reproduce code:
---------------
$t1 = floatval(8.95);
var_dump($t1);
t2 = $t1*100;
var_dump($t2);
$t3 = (int)$t2;
var_dump($t3);

Expected result:
----------------
t3 = 8.95

Actual result:
--------------
t3 = 8.94

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2009-07-08 20:35 UTC] jani@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.


 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Thu Jul 03 12:01:33 2025 UTC