rfc:new_rounding_modes_to_round_function

PHP RFC: Add 4 new rounding modes to round() function

Introduction

The RFC proposes to add 4 new modes to the round() function.

Two first comments on the documentation page of the round() function are related to the missing modes of rounding. The first comment has an outstanding number of 309 votes.

Moreover, the NumberFormatter already implements 4 new proposed modes known as ROUND_CEILING, ROUND_FLOOR, ROUND_DOWN, ROUND_UP.

Proposal

Adding 4 new constants for the rounding modes of function round():

  • PHP_ROUND_CEILING - rounds num to the nearest integer bigger than num,
  • PHP_ROUND_FLOOR - rounds num to the nearest integer lower than num,
  • PHP_ROUND_AWAY_FROM_ZERO - rounds num away from zero,
  • PHP_ROUND_TOWARD_ZERO - rounds num towards zero.

Creating two aliases of constants from Intl extension to have consistent naming for rounding modes:

  • ROUND_TOWARD_ZERO (equivalent of PHP_ROUND_TOWARD_ZERO) alias of ROUND_DOWN
  • ROUND_AWAY_FROM_ZERO (equivalent of PHP_ROUND_AWAY_FROM_ZERO) alias of ROUND_UP

Backward Incompatible Changes

None

Proposed PHP Versions

8.4

Questions

Why not use functions ceil() and floor()?

The functions ceil() and floor() round numbers to the full integer, while the function round() rounds the number by given precision. Implementing new rounding modes we get equivalent of those functions using round() with 0 as precision.

ceil($number) === round($number, 0, PHP_ROUND_CEILING); //true
floor($number) === round($number, 0, PHP_ROUND_FLOOR); //true

Why not the names PHP_ROUND_UP and PHP_ROUND_DOWN?

Such constants already exist in the NumberFormatter and represent rounding modes 'away from zero' and 'toward zero'. However the names rounding up and down are ambiguous. They may be easily confused with the rounding ceiling/floor, while the terms away from/toward zero are distinct.

Those constants would be deprecated and replaced with their corresponding aliases: ROUND_TOWARD_ZERO and ROUND_AWAY_FROM_ZERO.

Why not the enum implementation for the rounding modes?

As this is extension of the existing modes we should add complementary modes to the existing constants instead implementing new Enum. This way enum is an addition to the new constants and is not included into this RFC.

Proposed Voting Choices

As per the voting RFC a yes/no vote with a 2/3 majority is needed for this proposal to be accepted.

Voting started on 2023-11-15 and will end on 2023-11-30 00:00 GMT.

Implement 4 new rounding modes to `round()` function
Real name Yes No
ashnazg (ashnazg)  
bukka (bukka)  
crell (crell)  
derick (derick)  
galvao (galvao)  
girgias (girgias)  
joey (joey)  
kalle (kalle)  
levim (levim)  
mbeccati (mbeccati)  
nicolasgrekas (nicolasgrekas)  
nielsdos (nielsdos)  
ocramius (ocramius)  
petk (petk)  
ramsey (ramsey)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
theodorejb (theodorejb)  
timwolla (timwolla)  
Final result: 19 0
This poll has been closed.

If the first voting is not accepted the result of the second voting will be ignored.

Create aliases for Intl extension constants ROUND_UP and ROUND_DOWN: ROUND_AWAY_FROM_ZERO and ROUND_TOWARD_ZERO
Real name Yes No
ashnazg (ashnazg)  
bukka (bukka)  
crell (crell)  
derick (derick)  
galvao (galvao)  
girgias (girgias)  
joey (joey)  
kalle (kalle)  
levim (levim)  
mbeccati (mbeccati)  
nicolasgrekas (nicolasgrekas)  
ocramius (ocramius)  
ramsey (ramsey)  
santiagolizardo (santiagolizardo)  
sergey (sergey)  
theodorejb (theodorejb)  
Final result: 10 6
This poll has been closed.

Implementation

References

Links to external references, discussions or RFCs

Rejected Features

Keep this updated with features that were discussed on the mail lists.

rfc/new_rounding_modes_to_round_function.txt · Last modified: 2023/12/21 15:21 by jorg_sowa