php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Request #79073 round() does not have PHP_ROUND_UP or PHP_ROUND_DOWN modes
Submitted: 2020-01-07 10:48 UTC Modified: 2020-01-08 13:16 UTC
Votes:2
Avg. Score:3.0 ± 2.0
Reproduced:1 of 1 (100.0%)
Same Version:0 (0.0%)
Same OS:0 (0.0%)
From: objecttothis at gmail dot com Assigned:
Status: Open Package: *Math Functions
PHP Version: 7.4.1 OS: FreeBSD 11.2
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: objecttothis at gmail dot com
New email:
PHP Version: OS:

 

 [2020-01-07 10:48 UTC] objecttothis at gmail dot com
Description:
------------
There is no mode in round() that always rounds up or always rounds down.  To do this you have to use ceil() or floor() which do not use precision, so the result will always be an integer.  If PHP_ROUND_UP and PHP_ROUND_DOWN modes were added, it would resolve this problem.

Test script:
---------------
<?php
function round_up($number, $precision = 2)
{
    $fig = (int) str_pad('1', $precision, '0');
    return (ceil($number * $fig) / $fig);
}

function round_down($number, $precision = 2)
{
    $fig = (int) str_pad('1', $precision, '0');
    return (floor($number * $fig) / $fig);
}
?>


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-01-07 10:55 UTC] objecttothis at gmail dot com
The result, if these modes were added, would be:

echo round(1234.231,2,PHP_ROUND_UP); //Output 1234.24
echo round(1234.231,2,PHP_ROUND_DOWN); //Output 1234.23

When dealing with currency (accounting, discount calculation, etc) this is often needed.
 [2020-01-07 11:39 UTC] cmb@php.net
> When dealing with currency (accounting, discount calculation,
> etc) this is often needed.

I'd avoid float calculations for currency.
 [2020-01-07 12:00 UTC] objecttothis at gmail dot com
@cmb I'll bite.  What is the best practice for rounding currency if not a floating calculation?  Perhaps I'm missing something but if I need to get 51.5 percent of $11.01, the result will be $5.67015. Many times in use the desired rounding would be always up.  I really am curious how this should be done without a floating point calculation.
 [2020-01-08 13:16 UTC] cmb@php.net
The problem is not specificly the rounding, but rather that
floating point arithmetic has limited precision[1], so you may
have already a wrong result before rounding and rounding does not
necessarily fix that.

Currency calculations should better be done with BCMath or with
integers storing the currency in minor units (e.g. cents).
Rounding up is than as simple as adding 1 and truncating (for
integers), or adding "0.01" (for BCMath numbers).

And if you need division (or multiplication with non-integer
values), you may want to use rational numbers for intermediate
values.

[1] <https://floating-point-gui.de/>
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 26 00:01:30 2024 UTC