php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36039 intVal broken for calculated 1999
Submitted: 2006-01-17 00:57 UTC Modified: 2006-01-17 01:04 UTC
From: ben at bbbsystems dot net Assigned:
Status: Not a bug Package: *Math Functions
PHP Version: 5.1.2 OS: Free BSD 5.3
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: ben at bbbsystems dot net
New email:
PHP Version: OS:

 

 [2006-01-17 00:57 UTC] ben at bbbsystems dot net
Description:
------------
This bug is for the function intVal( ), and I also found that typecasting with (int) produces the same results.

This is a very weird bug I encountered because of a price that is 19.99 and our processor requires it to be sent as 1999.  1999 is the only value I was able to find that doesn't work correctly, but I'm sure there are others.

I also checked it on a linux system with an older version of php 4 and got the same results.  I upgraded our php to 5.1.2 then tried it again after it failed.  I suspect the bug to be a floating point issue, but I'm not certain of it.

-Ben Laurienti
BBB Systems, LLC

Reproduce code:
---------------
<?php
$test1 = intVal(1999);

$amount = 19.99 * 100;
$test2 = intVal($amount);//value is 1998
$test3 = intVal("$amount");//value is 1999

echo $test1 . "<br />\n";
echo $test2 . "<br />\n";
echo $test3 . "<br />\n";
?>

Expected result:
----------------
1999
1999
1999

Actual result:
--------------
1999
1998
1999

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-01-17 01:02 UTC] ben at bbbsystems dot net
The problem also happens with the function floor( )
 [2006-01-17 01:04 UTC] tony2001@php.net
Floating point values have a limited precision. Hence a value might 
not have the same string representation after any processing. That also
includes writing a floating point value in your script and directly 
printing it without any mathematical operations.

If you would like to know more about "floats" and what IEEE
754 is read this:
http://docs.sun.com/source/806-3568/ncg_goldberg.html
 
Thank you for your interest in PHP.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 13:01:31 2024 UTC