php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #15162 (int) cast doesn't work properly
Submitted: 2002-01-22 08:45 UTC Modified: 2002-01-22 10:25 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: slackware linux (kernel 2.2.19)
Private report: No CVE-ID: None
View Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
If you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: mik at x dot org dot pl
New email:
PHP Version: OS:

 

 [2002-01-22 08:45 UTC] 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

?>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-01-22 09:07 UTC] 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 10:25 UTC] jan@php.net
no bug in PHP, bogus
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Oct 31 23:01:28 2024 UTC