|  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
 [2012-06-21 23:41 UTC] iam4webwork at hotmail dot com
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:

Actual result:


Add a Patch

Pull Requests

Add a Pull Request


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]
-Status: Open +Status: Not a bug
 [2012-06-22 00:33 UTC]
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:

Thank you for your interest in PHP.

PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Jul 23 21:01:28 2024 UTC