Bug #15164 Updated: can php do a substraction? ;)
| From: | hholzgra@php.net | Date: | Tue, 22 Jan 2002 16:13:15 +0000 |
| Subject: | Bug #15164 Updated: can php do a substraction? ;) | ||
| References: | 1 | Groups: | php.dev |
| Request: | Send a blank email to php-dev+get-77789@lists.php.net to get a copy of this message | ||
ID: 15164
Updated by: hholzgra
Reported By: mik@x.org.pl
Status: Bogus
Bug Type: Math related
Operating System: linux 2.2.19
PHP Version: 4.1.1
New Comment:
lets call it what it is: an inevitable rounding error
that occurs when converting floating point values from
internal binary to human readable decimal format
use integers and count cents instead of dolars
or use http://php.net/number_format for output
as already suggested
by the way: the result is 56.999999..., not
57.999999..., so you are off by 0.000.....1
not 0.99999..... which is not so bad after
all
Previous Comments:
------------------------------------------------------------------------
[2002-01-22 11:02:04] mik@x.org.pl
what type should i do for currencies, so?
ok. let's don't call it a bug...
let's call it for example a little magic of floats.
but don't u think u could correct it?
------------------------------------------------------------------------
[2002-01-22 10:29:10] jan@php.net
floats are not supposed to be exact (per definition). do not use them
on currencies. calculate cents ant use (agein) number_format() to
display them prooperly. No bug in PHP, bogus.
------------------------------------------------------------------------
[2002-01-22 09:17:32] mik@x.org.pl
my previous bug report was 15162. i wrote about (int) cast there.
another bug may be a reason of that.
i do: (267.57 - 267) * 100
and i get: 57.999999999999
(i expected 57)
[may be i work too much...]
consider this:
<?
$price = 267.57;
$price_dolars = floor($price);
echo "number of cents is: " . ($price - $price_dolars)*100 . "<BR>";
?>
------------------------------------------------------------------------
Edit this bug report at http://bugs.php.net/?id=15164&edit=1