Bug #15162 Updated: (int) cast doesn't work properly
| From: | jan@php.net | Date: | Tue, 22 Jan 2002 15:25:40 +0000 |
| Subject: | Bug #15162 Updated: (int) cast doesn't work properly | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-77778@lists.php.net to get a copy of this message | ||
ID: 15162
Updated by: jan
Old Summary: (int) cast doesn't work properly
Reported By: mik@x.org.pl
Old Status: Open
Status: Bogus
Bug Type: Math related
Operating System: slackware linux (kernel 2.2.19)
PHP Version: 4.1.1
New Comment:
no bug in PHP, bogus
Previous Comments:
------------------------------------------------------------------------
[2002-01-22 09:07:26] daniel@php.net
Or to give a shorter example:
<pre><?
$test = (1.2 - 1) * 10;
echo var_dump($test);
echo var_dump((int)$test);
?></pre>
which outputs:
float(2)
int(1)
This only happens when a subtraction is made. when (1.2 - 1) is
replaced by 0.2, this feature doesn't show up.
Either I really overlooked something or this is a bug.
Anyway, I suggest you to calculate only with cents - which also allows
easier calculating. Formatting can be done later.
$price = 3340;
then use number_format() to format.
Kind Regards,
Daniel Lorch
------------------------------------------------------------------------
[2002-01-22 08:45:33] mik@x.org.pl
i'm writing a little internet shop in php.
i found really bad bug, which can make a lot of losses to somebody...
may be i'm using cast not good way, but i call this bug anyway.
consider this script:
<?
$price = 33.44;
echo "the price is: $price <BR>";
$price_dolars = floor($price);
echo "number of cents is: " . ($price - $price_dolars)*100 . "<BR>";
// we can see value of 44
echo "but as integer it would be: " . (int)(($price -
$price_dolars)*100) . "<BR>";
// but now it is 43
?>
------------------------------------------------------------------------
Edit this bug report at http://bugs.php.net/?id=15162&edit=1