ID:               36008
 Updated by:       [EMAIL PROTECTED]
 Reported By:      adi at rogers dot com
-Status:           Open
+Status:           Bogus
 Bug Type:         Math related
 Operating System: Windows XP or Server 2003
 PHP Version:      5.1.2
 New Comment:

Output on my machine using default precision 12:
Wrong: 14.38
Wrong: 14.38
'Magically' correct: 14.38
'Magically' correct: 14.38
Converted to string and is now correct: 14.38
Converted to string and is now correct: 14.38

And it works exactly like it's supposed to.
round() rounds up to 2 decimals. Same goes for number_format(). 

14.375 == 14.38 but 14.374 == 14.37

There is no bug here, and please, don't bother reopening this yet
again.


Previous Comments:
------------------------------------------------------------------------

[2006-01-14 05:15:52] adi at rogers dot com

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 this bug report at http://bugs.php.net/?id=36008&edit=1

Reply via email to