From:             adi at rogers dot com
Operating system: Windows XP or Server 2003
PHP version:      5.1.2
PHP Bug Type:     Math related
Bug description:  Mishandled type 'double' causes incorrect round() & 
number_format() output

Description:
------------
Very lame/unreliable ;)
Both round() and number_format() succumb to this problem.
They cannot handle float or double variables properly even though those
variable types are "flawed / limited" as apparently described numerous
times on here by PHP personnel. Well my code below appears to showcase the
bug which will hopefully not be shot off as "bogus" like many times before
as reported in the past by various people.

I found a work-around which I will now be using. Convert your value to a
string variable before passing it to the round() or number_format()
function.

Reproduce code:
---------------
$num=12.5*1.15;    // Produces 14.375 as type: double.
$num_same_thing=14.375; // Produces 14.375 as type: double.
echo "Wrong: ".round($num, 2)."\r\n";
echo "Wrong: ".number_format($num, 2)."\r\n";
echo "'Magically' correct: ".round($num_same_thing, 2)."\r\n";
echo "'Magically' correct: ".number_format($num_same_thing, 2)."\r\n";
// The above two variables are the same value and same type, yet are
rounded differently.
settype($num, "string");
// Now that $num is a string, we get the right result.
echo "Converted to string and is now correct: ".round($num, 2)."\r\n";
echo "Converted to string and is now correct: ".number_format($num,
2)."\r\n";

Expected result:
----------------
The functions should have outputted 14.38 no matter how the variables of
type 'double' were assigned the value 14.375.


-- 
Edit bug report at http://bugs.php.net/?id=36008&edit=1
-- 
Try a CVS snapshot (PHP 4.4): 
http://bugs.php.net/fix.php?id=36008&r=trysnapshot44
Try a CVS snapshot (PHP 5.1): 
http://bugs.php.net/fix.php?id=36008&r=trysnapshot51
Try a CVS snapshot (PHP 6.0): 
http://bugs.php.net/fix.php?id=36008&r=trysnapshot60
Fixed in CVS:                 http://bugs.php.net/fix.php?id=36008&r=fixedcvs
Fixed in release:             
http://bugs.php.net/fix.php?id=36008&r=alreadyfixed
Need backtrace:               http://bugs.php.net/fix.php?id=36008&r=needtrace
Need Reproduce Script:        http://bugs.php.net/fix.php?id=36008&r=needscript
Try newer version:            http://bugs.php.net/fix.php?id=36008&r=oldversion
Not developer issue:          http://bugs.php.net/fix.php?id=36008&r=support
Expected behavior:            http://bugs.php.net/fix.php?id=36008&r=notwrong
Not enough info:              
http://bugs.php.net/fix.php?id=36008&r=notenoughinfo
Submitted twice:              
http://bugs.php.net/fix.php?id=36008&r=submittedtwice
register_globals:             http://bugs.php.net/fix.php?id=36008&r=globals
PHP 3 support discontinued:   http://bugs.php.net/fix.php?id=36008&r=php3
Daylight Savings:             http://bugs.php.net/fix.php?id=36008&r=dst
IIS Stability:                http://bugs.php.net/fix.php?id=36008&r=isapi
Install GNU Sed:              http://bugs.php.net/fix.php?id=36008&r=gnused
Floating point limitations:   http://bugs.php.net/fix.php?id=36008&r=float
No Zend Extensions:           http://bugs.php.net/fix.php?id=36008&r=nozend
MySQL Configuration Error:    http://bugs.php.net/fix.php?id=36008&r=mysqlcfg

Reply via email to