Member-only story
šµ 4 New Rounding Methods Likely In PHP 8.4

PHP 8.4 may come with four new methods of rounding numbers for fine-tuned statistical analysis of data
You could say rounding is my pony. Actually after writing the article about it in January i almost forgot about the issue.
The issue being that thereās no onr standard way of rounding. Actually thereās a lot of rounding methods.
Specifically when your value is exactly between two integers, like 8.5, it can be rounded based on several rules that are different in various languages. Some round up (ceiling), down (floor), away from zero, toward zero, to closest even or to closest odd number.
Itās important how you round numbers because it impacts operations on numbers.
For example if you round 1.5 and 2.5 to ceiling you get 2 and 3. An average of the sum of these numbers is 5 / 2 = 2.5.
However, the exact average is 4/2 = 2.
On the other hand when you round to even, you get 2 and 2. Average is 2.
As you can see the value is closer to the real value than rounding to ceiling.
It means that itās important how you round your data.
So Iāve almost forgot about that topic until Iāve found the RFC for PHP 8.4
The proposal of Jorg Sowa four ways to cut the tie of rounding:
- 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.
It will supplement already available methods of rounding:
- PHP_ROUND_HALF_UP Rounds num away from zero when it is half way there, making 1.5 into 2 and -1.5 into -2.
- PHP_ROUND_HALF_DOWN Rounds num towards zero when it is half way there, making 1.5 into 1 and -1.5 into -1.
- PHP_ROUND_HALF_EVEN Rounds num towards the nearest even value when it is half way there, making both 1.5 and 2.5 into 2.
- PHP_ROUND_HALF_ODD Rounds num towards the nearest odd value when it is half way there, making 1.5 into 1 and 2.5 into 3.
Itās expected that the new rounding methods will be available in PHP 8.4. The voting on the change was pretty uniform with some mixed results of technical organisation that should not impact the solution as it is.
That way weāll have 9 rounding methods satisfying the tastes of the most demanding rounders like the author of this text.
Whatās your favorite rounding method? Mine is rounding to odd, itās fancy š
If you like Expanse TV series subscribe to my articles āŗļø Or coding š