php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78172 Wrong rounding up/down when decimal places are specified
Submitted: 2019-06-16 16:18 UTC Modified: 2019-06-16 17:04 UTC
From: vecera at zdenekvecera dot cz Assigned: cmb (profile)
Status: Not a bug Package: *Math Functions
PHP Version: 7.1.30 OS: Windows, Linux
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: vecera at zdenekvecera dot cz
New email:
PHP Version: OS:

 

 [2019-06-16 16:18 UTC] vecera at zdenekvecera dot cz
Description:
------------
var_dump(round(1.44444, 3, PHP_ROUND_HALF_DOWN)); // float(1.444) OK 
var_dump(round(1.55555, 3, PHP_ROUND_HALF_DOWN)); // float(1.556) BUG (correct value: 1.555)
var_dump(round(1.77777, 3, PHP_ROUND_HALF_DOWN)); // float(1.778) BUG (correct value: 1.777)

var_dump(round(1.44444, 3, PHP_ROUND_HALF_UP)); // float(1.444) BUG (correct value: 1.445)
var_dump(round(1.55555, 3, PHP_ROUND_HALF_UP)); // float(1.556) OK
var_dump(round(1.77777, 3, PHP_ROUND_HALF_UP)); // float(1.778) OK

// PHP version: 7.0.33, 7.1.30, 7.2.19, 7.3.6


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-06-16 17:00 UTC] danack@php.net
I don't understand what how you get your expected values.

// round(1.44444, 3, PHP_ROUND_HALF_UP) (correct value: 1.445)

That would be the same as expected 0.00044 rounded to 3 digits to be 0.001. but 0.00044 is less than 0.0005 which is the halfway point to 0.001.


Similarly 0.00055 rounded to 3 figures is going to be 0.001 no matter the rounding.
 [2019-06-16 17:04 UTC] cmb@php.net
-Status: Open +Status: Not a bug -Assigned To: +Assigned To: cmb
 [2019-06-16 17:04 UTC] cmb@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to report
a bug at http://bugs.php.net/how-to-report.php

It's like @danack said; IOW the PHP_ROUND_HALF_* constants are
only relevant when the fractional part of $val * 10**$precision is
exactly 0.5.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Oct 27 16:01:27 2024 UTC