#36008 [Opn->Fbk]: Mishandled type 'double' causes incorrect round() & number_format() output
| From: | sniper@php.net | Date: | Sat, 14 Jan 2006 15:06:26 +0000 |
| Subject: | #36008 [Opn->Fbk]: Mishandled type 'double' causes incorrect round() & number_format() output | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-91911@lists.php.net to get a copy of this message | ||
ID: 36008
Updated by: sniper@php.net
Reported By: adi at rogers dot com
-Status: Open
+Status: Feedback
Bug Type: Math related
Operating System: win32 only
PHP Version: 5.1.2
New Comment:
What EXACTLY is the output when you set precision to 32?
Previous Comments:
------------------------------------------------------------------------
[2006-01-14 15:55:07] adi at rogers dot com
I just confirmed this issue to still exist on the following systems:
Windows XP Pro SP2 [IIS 5.1 ISAPI] (PHP 5.1.2 / php.ini precision: 12)
Windows XP Pro SP2 [IIS 5.1 CGI] (PHP 5.1.0RC1 / php.ini precision:
12)
Windows Server 2003 Enterprise x64 Edition SP1 [IIS 6.0 CGI] (PHP 5.1.2
/ php.ini precision: 12)
PHP was installed on all three systems manually (not using the
installer).
Hope that helps in isolating this issue.
------------------------------------------------------------------------
[2006-01-14 11:16:57] sniper@php.net
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.
------------------------------------------------------------------------
[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