[Django] #31487: Add support for precision argument to Round

37 views
Skip to first unread message

Django

unread,
Apr 20, 2020, 9:27:54 AM4/20/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste | Owner: nobody
Mispelon |
Type: New | Status: new
feature |
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
Django's `Round` function currently only supports rounding to the nearest
integer. If you need to round to a more specific precision you need to
roll out your own solution.

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.

Django

unread,
Apr 20, 2020, 9:48:01 AM4/20/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: nobody
Type: New feature | Status: new
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:1>

Django

unread,
Apr 20, 2020, 10:41:25 AM4/20/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: nobody => Hasan Ramezani
* status: new => assigned


--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:2>

Django

unread,
Apr 20, 2020, 12:04:06 PM4/20/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* 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>

Django

unread,
Apr 21, 2020, 4:33:10 AM4/21/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Apr 21, 2020, 4:40:52 AM4/21/20
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Hasan
| Ramezani
Type: New feature | Status: assigned
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

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>

Django

unread,
Jan 7, 2021, 5:33:05 AM1/7/21
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: (none)

Type: New feature | Status: new
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Hasan Ramezani):

* owner: Hasan Ramezani => (none)
* status: assigned => new


--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:6>

Django

unread,
Mar 25, 2021, 5:18:05 AM3/25/21
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Nick Pope

Type: New feature | Status: assigned
Component: Database layer | Version: dev

(models, ORM) |
Severity: Normal | Resolution:
Keywords: database function, | Triage Stage: Accepted
round, precision, decimal places |
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Nick Pope):

* 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>

Django

unread,
Mar 26, 2021, 4:39:30 AM3/26/21
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: database function, | Triage Stage: Accepted
round, precision, decimal places |
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 1

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:8>

Django

unread,
Mar 29, 2021, 3:40:37 AM3/29/21
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Nick Pope
Type: New feature | Status: assigned
Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution:
Keywords: database function, | Triage Stage: Ready for
round, precision, decimal places | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak):

* needs_better_patch: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31487#comment:9>

Django

unread,
Mar 29, 2021, 4:15:14 AM3/29/21
to django-...@googlegroups.com
#31487: Add support for precision argument to Round
-------------------------------------+-------------------------------------
Reporter: Baptiste Mispelon | Owner: Nick Pope
Type: New feature | Status: closed

Component: Database layer | Version: dev
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: database function, | Triage Stage: Ready for
round, precision, decimal places | checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* 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>

Reply all
Reply to author
Forward
0 new messages