php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #62385 intval gives wrong result with float value
Submitted: 2012-06-21 23:41 UTC Modified: 2012-06-22 00:33 UTC
From: iam4webwork at hotmail dot com Assigned:
Status: Not a bug Package: Math related
PHP Version: 5.4.4 OS:
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: iam4webwork at hotmail dot com
New email:
PHP Version: OS:

 

 [2012-06-21 23:41 UTC] iam4webwork at hotmail dot com
Description:
------------
A float value of 19.99 * 100 when passed to intval becomes 1998 unless embedded in 
a string.


Test script:
---------------
$amount = 19.99 * 100;
$test2 = intVal($amount);
$test3 = intVal("$amount");

echo $test2 . "<br />\n";
echo $test3 . "<br />\n";


Expected result:
----------------
1999
1999

Actual result:
--------------
1998
1999

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-06-21 23:43 UTC] iam4webwork at hotmail dot com
It only works correctly if you round the float value and then pass it to intval.
 [2012-06-22 00:33 UTC] rasmus@php.net
-Status: Open +Status: Not a bug
 [2012-06-22 00:33 UTC] rasmus@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://www.floating-point-gui.de/

Thank you for your interest in PHP.

.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Dec 03 17:01:29 2024 UTC