From:             productivepc at hotmail dot com
Operating system: Windows 7
PHP version:      5.5.3
Package:          Math related
Bug Type:         Bug
Bug description:Using division PHP_ROUND_HALF_UP rounds DOWN when working with 
2 precision

Description:
------------
PHP Version 5.5.0
Results on Screen when precision is set to 2 for $additionalPcCostPerDay.

mainPcCost: 169
additionalPcCost: 3.3328571428571
WorkingDaysLeft: 16
totalWorkingDays: 21
mainPcCostPerDay: 8.05
additionalPcCostPerDay: 3.333
mainPcProratedCost: 128.8
additionalPcProratedCost: 53.33
totalProratedCost: 182.13

=============================================
Results on screen when I change the precision to 2.  Please notice
everything 
has stayed the same but the totalProratedCost has changed.  
contracttermid: 2
businesstypeid: 1
mainPcCost: 169
additionalPcCost: 3.3328571428571
WorkingDaysLeft: 16
totalWorkingDays: 21
mainPcCostPerDay: 8.05
additionalPcCostPerDay: 3.33
mainPcProratedCost: 128.8
additionalPcProratedCost: 53.28
totalProratedCost: 182.08

I noticed something because I have this same thing programmed in excel.  I

decided to add the ROUNDUP function to my excel document just like I have
it in 
PHP to ensure that everything worked exactly the same and I noticed that
when I 
went to 2 decimals excel actually changed the additionalPcCostPerDay to
3.34 
instead of keeping it 3.33 therefore raising the price to 182.24 and PHP
brought 
the price down to 182.08 instead of keeping it 182.13.  Looking in to this

further, if I use the ROUNDDOWN function 
within excel then it too brings the price to 182.08 which is what makes me

believe that the PHP function PHP_ROUND_HALF_UP is actually ROUNDING DOWN 
instead of UP.  The 
expected behavior for both excel and PHP would be to leave the price at
182.13.  
When I use 3 decimals for both then both PHP and Excel agree that the 
totalProratedPrice is 182.13.  I am not sure if this is a bug however I
wanted 
to report it here before making a comment on the ROUND page with an example
of 
this.

The example below I do not have settype() in it however I have attempted
this by 
changing everything over to a float with settype before I did any math and
the 
result was still the same.

Wayne

Test script:
---------------
$workingDaysLeft = 16;
$totalWorkingDays = 21;
$mainPcCost = 169;
$additionalPcCost = 3.3328571428571;
$mainPcCostPerDay = round($mainPcCost/$totalWorkingDays,2,
PHP_ROUND_HALF_UP);
$additionalPcCost = $additionalPcCost/$totalWorkingDays;

// The next line is the problem area.  When I change it to 2 precision the
expected behavior is for the total prorated cost to stay at 182.13 however
it drops to 182.08.  If you change the below line to PHP_ROUND_HALF_DOWN
then you also get 182.08 which would indicate that the behavior is not
behaving as expected.
$additionalPcCostPerDay = round($additionalPcCost,3,PHP_ROUND_HALF_UP);

$mainPcProratedCost =
round($mainPcCostPerDay*$workingDaysLeft,2,PHP_ROUND_HALF_UP);
$additionalPcProratedCost =
round($additionalPcCostPerDay*$workingDaysLeft,2,PHP_ROUND_HALF_UP);
$totalProratedCost =
number_format($mainPcProratedCost+$additionalPcProratedCost,2,'.',',');
echo "<br>mainPcCost: " . $mainPcCost . "<br>additionalPcCost: " .
$additionalPcCost;
echo "<br>WorkingDaysLeft: " . $workingDaysLeft . "<br>totalWorkingDays: "
. $totalWorkingDays;
echo "<br>mainPcCostPerDay: " . $mainPcCostPerDay .
"<br>additionalPcCostPerDay: " . $additionalPcCostPerDay;
echo "<br>mainPcProratedCost: " . $mainPcProratedCost .
"<br>additionalPcProratedCost: " . $additionalPcProratedCost;
echo "<br>totalProratedCost: " . $totalProratedCost;

Expected result:
----------------
Whether I have precision for $additionalPcCostPerDay set to 2 or 3 the 
totalProratedCost should remain the 182.13 and not change to 182.08.


-- 
Edit bug report at https://bugs.php.net/bug.php?id=65637&edit=1
-- 
Try a snapshot (PHP 5.4):   
https://bugs.php.net/fix.php?id=65637&r=trysnapshot54
Try a snapshot (PHP 5.3):   
https://bugs.php.net/fix.php?id=65637&r=trysnapshot53
Try a snapshot (trunk):     
https://bugs.php.net/fix.php?id=65637&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=65637&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=65637&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=65637&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=65637&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=65637&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=65637&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=65637&r=notwrong
Not enough info:            
https://bugs.php.net/fix.php?id=65637&r=notenoughinfo
Submitted twice:            
https://bugs.php.net/fix.php?id=65637&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=65637&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=65637&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=65637&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=65637&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=65637&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=65637&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=65637&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=65637&r=mysqlcfg

Reply via email to