Bug #70447 [NEW]: Wrong float rounding using round function
| From: | m dot mochetti at design-it dot de | Date: | Mon, 07 Sep 2015 11:14:26 +0000 |
| Subject: | Bug #70447 [NEW]: Wrong float rounding using round function | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-195838@lists.php.net to get a copy of this message | ||
From: m dot mochetti at design-it dot de
Operating system: Linux (ubuntu)
PHP version: 5.5.29
Package: *General Issues
Bug Type: Bug
Bug description:Wrong float rounding using round function
Description:
------------
I'm having a serious issue regarding prices not being properly rounded
in a sales platform the company I work for is currently developing.
Please take a look at the Test script attached, it showcases the issue.
Test script:
---------------
<?php
$float = 87.755;
$units = (int)floor($float);
$cents = (float)($float - $units);
echo "UNITS = ".$units; // UNITS = 87
echo "<br>";
echo "CENTS = ".$cents; // CENTS = 0.755
echo "<br>";
echo "ROUNDED CENTS = ".round($cents, 2); // ROUNDED CENTS = 0.75 -->
WRONG!!
echo "<br>";
echo "ROUNDED CENTS (without using variable): ".round(0.755, 2); //
ROUNDED CENTS (without var) : 0.76 --> CORRECT!!
?>
Expected result:
----------------
round(0.755, 2) should return the same result as round($var, 2) // being
$var a calculated value that ended up to be 0.755
Actual result:
--------------
round(0.755,2) = 0.76
round($var, 2) = 0.75
--
Edit bug report at https://bugs.php.net/bug.php?id=70447&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=70447&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=70447&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=70447&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=70447&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=70447&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=70447&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=70447&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=70447&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=70447&r=support
Expected behavior: https://bugs.php.net/fix.php?id=70447&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=70447&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=70447&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=70447&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=70447&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=70447&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=70447&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=70447&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=70447&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=70447&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=70447&r=mysqlcfg