But as far as I can tell, all backends natively supported by Django
support a second argument to `Round`:
* Postgres: https://www.postgresql.org/docs/12/functions-math.html
#FUNCTIONS-MATH-FUNC-TABLE
* Sqlite: https://sqlite.org/lang_corefunc.html#round
* MySQL: https://dev.mysql.com/doc/refman/8.0/en/mathematical-
functions.html#function_round
* Oracle:
https://docs.oracle.com/cd/B19306_01/server.102/b14200/functions135.htm
In my project (postgres only) my work around was to declare my own custom
function:
{{{#!python
class Round(Func):
function = 'ROUND'
arity = 2
}}}
But having this built-in would be quite useful.
--
Ticket URL: <https://code.djangoproject.com/ticket/31487>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
* stage: Unreviewed => Accepted
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:1>
* owner: nobody => Hasan Ramezani
* status: new => assigned
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:2>
* cc: Nick Pope (added)
Comment:
Please take into account that it was
[https://github.com/django/django/pull/9622#issuecomment-401628781
discussed] in the original [https://github.com/django/django/pull/9622
PR], and we decided to leave `Round()` without a precision because it can
be unstable around `0.5`.
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:3>
Comment (by Baptiste Mispelon):
Replying to [comment:3 felixxm]:
> Please take into account that it was
[https://github.com/django/django/pull/9622#issuecomment-401628781
discussed] in the original [https://github.com/django/django/pull/9622
PR], and we decided to leave `Round()` without a precision because it can
be unstable around `0.5`.
What does "unstable around 0.5" mean? I assume it has something to do with
what Python call rounding modes [1] but if that's the case I don't
understand why that wouldn't also apply to rounding to integers.
The comment you link doesn't have a lot of information and itself links to
another comment about Spatialite which doesn't really clarify much.
Navigating the discussion on the PR is a bit hard but searching for the
word "round" in the commit history of the PR I found
https://github.com/django/django/pull/9622/commits/5acc1162a716c4ff6fcb0bca32840e11d6585c7e.
The warning added in that commit seems like it was not integrated into the
final commit (I searched Django's history with `git log -S "equally close"
-- docs/` to try and find it) but I can't find a comment explaining the
decision to remove it.
[1] https://docs.python.org/3/library/decimal.html#rounding-modes
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:4>
Comment (by felixxm):
> The comment you link doesn't have a lot of information and itself links
to another comment about Spatialite which doesn't really clarify much.
Agreed, that's why I CC'ed Nick, maybe he remember rationales.
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:5>
* owner: Hasan Ramezani => (none)
* status: assigned => new
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:6>
* keywords: => database function, round, precision, decimal places
* owner: (none) => Nick Pope
* has_patch: 0 => 1
* version: 3.0 => dev
* status: new => assigned
Comment:
For once, I can't remember exactly why we didn't do this, hence my opening
of the duplicate ticket #32592 yesterday looking to fix exactly this
issue.
I can think of a number of possible reasons based on the discussions and
looking at the existing code:
- Something to do with the rounding issue around `0.5`. ''(I don't think
this should stop us supporting the second argument. We already mention it
in the [https://docs.djangoproject.com/en/3.1/ref/models/database-
functions/#round documentation].)''
- Something to do with `Round` being a `Transform` which sets `arity = 1`.
''(This can be overridden back to `None` while still allowing the
transform to work.)''
- Something to do with SpatiaLite. I'm not sure what that comment was
about as SQLite and SpatialLite behave the same and the function is
available in SQLite.
- The SQLite version of `ROUND()` does not support negative precision
values.
Anyway, here is a [https://github.com/django/django/pull/14182 PR].
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:7>
* needs_better_patch: 0 => 1
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:8>
* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:9>
* status: assigned => closed
* resolution: => fixed
Comment:
In [changeset:"2f13c476abe4ba787b6cb71131818341911f43cc" 2f13c476]:
{{{
#!CommitTicketReference repository=""
revision="2f13c476abe4ba787b6cb71131818341911f43cc"
Fixed #31487 -- Added precision argument to Round().
}}}
--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:10>