Implement PHP_ROUND_UP and PHP_ROUND_DOWN options for round_updown.ph… #1658
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
PHP PR - Additional Rounding modes for the round() function
This PR adds new rounding modes to
round()
forPHP_ROUND_UP
andPHP_ROUND_DOWN
that can take advantage of the precision argument to round up or down to a specified number of decimal places.Introduction
There has been a demand for this functionality for some time, with various of the user comments on the docs page for
round()
specifically requesting it, such as that posted bytakingsides at gmail dot com
or other comment postings by others showing how the functionality can be implemented in userland code. Similar comments appear in the PHP docs pages for
ceil()
andfloor()
; and this is also a regular question tagged against PHP on StackOverflow.While the mathematics is simple enough to implement in userland code, it would certainly be useful if it was actually implemented in PHP core, and the
round()
function is the logical place to do so, as it already implements precision and a mode flag that identifies a rounding method to use.Proposal
The existing
round()
function already has precision logic, and provides a mode option allowing userland code to identify a method that will be applied to the rounding, but this only affects the behaviour of half (.5) values identifying whether they should be rounded up or down. Existing mode options aree.g
This proposal is to add new mode options for round() (PHP_ROUND_UP and PHP_ROUND_DOWN) and the appropriate code logic to apply ceiling and floor for the value to be rounded to the precision specified in the function call.
Behaviour for
PHP_ROUND_DOWN
andPHP_ROUND_UP
will mimicceil()
andfloor()
, soPHP_ROUND_UP
will round positive numbers toward infinity and negative numbers toward 0, whilePHP_ROUND_DOWN
will round positive numbers toward 0 and negative numbers toward -infinity.Backward Incompatible Changes
This modification would not affect backward compatibility in any way. The existing mode flags and default behaviour would remain unchanged
Proposed PHP Version(s)
As there is no change to backward compatibility, this can be targetted at PHP.next or any intermediate release
Optionally Extending the Functionality
With minimal changes, it would also be possible to implement
PHP_ROUND_TOWARD_ZERO
andPHP_ROUND_AWAY_FROM_ZERO
flags; wherePHP_ROUND_TOWARD_ZERO
would round numbers toward 0, whether positive or negative values, whereasPHP_ROUND_AWAY_FROM_ZERO
would round positive values toward infinity and negative numbers toward -infinity.Alternative Proposal
An alternative approach to provide the same functionality would be to add optional precision arguments to the
ceil()
andfloor()
functions, with a default value of 0 to ensure that backward compatibility was not affected.References
http://www.php.net/manual/en/function.round.php
http://www.php.net/manual/en/function.ceil.php
http://www.php.net/manual/en/function.floor.php