Bug #62385 [Opn->Nab]: intval gives wrong result with float value

From: Date: Fri, 22 Jun 2012 00:33:47 +0000
Subject: Bug #62385 [Opn->Nab]: intval gives wrong result with float value
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-171421@lists.php.net to get a copy of this message
Edit report at https://bugs.php.net/bug.php?id=62385&edit=1 ID: 62385 Updated by: rasmus@php.net Reported by: iam4webwork at hotmail dot com Summary: intval gives wrong result with float value -Status: Open +Status: Not a bug Type: Bug Package: Math related PHP Version: 5.4.4 Block user comment: N Private report: N New Comment: 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. . Previous Comments: ------------------------------------------------------------------------ [2012-06-21 23:43:57] iam4webwork at hotmail dot com It only works correctly if you round the float value and then pass it to intval. ------------------------------------------------------------------------ [2012-06-21 23:41:56] 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 ------------------------------------------------------------------------ -- Edit this bug report at https://bugs.php.net/bug.php?id=62385&edit=1

« previous php.bugs (#171421) next »