|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #10313 ROUND inconsistency
Submitted: 2001-04-12 22:20 UTC Modified: 2001-04-13 14:35 UTC
From: faxguy at deanox dot com Assigned:
Status: Closed Package: *Math Functions
PHP Version: 4.0.4pl1 OS: RedHat Linux 7.0
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
12 - 1 = ?
Subscribe to this entry?

 [2001-04-12 22:20 UTC] faxguy at deanox dot com
Using MySQL 3.23.32 on RedHat Linux 7.0...

MySQL's ROUND function rounds 5 up when the preceding digit is odd and down when the preceding digit is even.

mysql> select round(1.5);
| round(1.5) |
|          2 |
1 row in set (0.00 sec)

mysql> select round(2.5);
| round(2.5) |
|          2 |
1 row in set (0.00 sec)

I think that this is technically the correct behavior, scientifically, anyway.  However, this is not the common "lay-man's" method of rounding, which is to always round 5 up, as exhibited by PHP-4.0.4pl1...

	<? echo round(1.5); ?>
	<? echo round(2.5); ?>

Apache 1.3.14 output for this is:


This discrepancy causes a difficulty in programming PHP and MySQL together, for example, because all of the rounding must be done in either PHP or MySQL but not both partially unless you want conflicting data.

I would like to see MySQL ROUND() syntax expand to be ROUND(X,D,M) where optional M value indicates the method of rounding, the default being the current method.

I would also like to see PHP round() syntax expand to be 
	double round (double val [, int precision] [, char method])
where the optional method value indicates the method of rounding, the default being the current method.


Lee Howard


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2001-04-13 14:35 UTC]
This isn't a bug. PHP uses the underlying C library to round, and all known C library's do this the same.
Actually, you are reporting a bug for MySQL, so please ask on there bug list for this.
 [2004-09-06 01:35 UTC] tivnet at mail dot com
The problem still exists - September 2004, latest PHP4 and MySql 4.0
 [2004-09-06 01:53 UTC] tivnet at mail dot com
The quick workaround seems to be using TRUNCATE in MySQL:
TRUNCATE( 38.50 + 0.5, 0 ) = 39
ROUND( 38.50, 0 ) = 38

PHP Copyright © 2001-2022 The PHP Group
All rights reserved.
Last updated: Tue Sep 27 05:05:53 2022 UTC