rounding integers
| From: | Marc | Date: | Sun, 21 May 2023 05:16:35 +0000 |
| Subject: | rounding integers | ||
| Groups: | php.internals | ||
| Request: | Send a blank email to internals+get-120373@lists.php.net to get a copy of this message | ||
Hi all,
The "round" function supports rounding to significant digits before the decimal point which can be very helpful
but unfortunately it's handling everything as floating point numbers which in the end can lead to inaccurate results.
printf("%d\n", round(987654321098765432, -1) // 987654321098765440 instead of 987654321098765430
To be fair it's also documented as such:
round — Rounds a floatround(int|float $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): float I would like to propose to change this function to keep the given type and proper handle integers as well:
round — Rounds a number/** @template T of int|float */ round(T $num, int $precision = 0, int $mode = PHP_ROUND_HALF_UP): T Do you think this could be an acceptable BC-break or should this be a different function? Thanks, Marc