#15162 [Com]: (int) cast doesn't work properly
| From: | bmorel at ssi dot fr | Date: | Wed, 01 Sep 2004 21:48:21 +0000 |
| Subject: | #15162 [Com]: (int) cast doesn't work properly | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-65592@lists.php.net to get a copy of this message | ||
ID: 15162
Comment by: bmorel at ssi dot fr
Reported By: mik at x dot org dot pl
Status: Bogus
Bug Type: Math related
Operating System: slackware linux (kernel 2.2.19)
PHP Version: 4.1.1
New Comment:
(Using PHP 4.3.3 WinXP & PHP 4.3.6 Linux Redhat)
Reproduce this kind of bug by simply doing this :
echo (int)( 100 * "19.90" );
// writes 1989 :-(
Is this bug corrected ?
Previous Comments:
------------------------------------------------------------------------
[2002-01-22 10:25:40] jan@php.net
no bug in PHP, bogus
------------------------------------------------------------------------
[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 at x dot org dot 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