php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15164 can php do a substraction? ;)
Submitted: 2002-01-22 09:17 UTC Modified: 2002-01-22 11:13 UTC
Votes:1
Avg. Score:5.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: mik at x dot org dot pl Assigned:
Status: Not a bug Package: Math related
PHP Version: 4.1.1 OS: linux 2.2.19
Private report: No CVE-ID: None
 [2002-01-22 09:17 UTC] mik at x dot org dot 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>";
?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-22 10:29 UTC] 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 11:02 UTC] mik at x dot org dot 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 11:13 UTC] hholzgra@php.net
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

 [2002-01-23 04:38 UTC] mik at x dot org dot pl
thanks very much for explaining. i haven't link teory with practice (about floats). probably i realy work too much. :)

let me ask one more question:
if i store prices in postgresql as numeric(10,2), how can i secure handle it in php?
could i do: 
$price_c = (int)($psql_num_price * 100); //?
and how to write it back?
$psql_num_price = $price_c / 100; //?

thanks one more time.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Dec 06 07:01:29 2024 UTC