Opened 4 months ago

Last modified 4 months ago

#31487 assigned New feature

Add support for precision argument to Round

Reported by: Baptiste Mispelon Owned by: Hasan Ramezani
Component: Database layer (models, ORM) Version: 3.0
Severity: Normal Keywords:
Cc: Nick Pope Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

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:

In my project (postgres only) my work around was to declare my own custom function:

class Round(Func):
    function = 'ROUND'
    arity = 2

But having this built-in would be quite useful.

Change History (5)

comment:1 Changed 4 months ago by Simon Charette

Triage Stage: UnreviewedAccepted

comment:2 Changed 4 months ago by Hasan Ramezani

Owner: changed from nobody to Hasan Ramezani
Status: newassigned

comment:3 Changed 4 months ago by felixxm

Cc: Nick Pope added

Please take into account that it was discussed in the original PR, and we decided to leave Round() without a precision because it can be unstable around 0.5.

comment:4 in reply to:  3 Changed 4 months ago by Baptiste Mispelon

Replying to felixxm:

Please take into account that it was discussed in the original 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.

Edit
While searching for more information about rounding modes in different databases, I found a useful reference of different rounding modes and their results when applied to various inputs: https://docs.oracle.com/javase/7/docs/api/java/math/RoundingMode.html

[1] https://docs.python.org/3/library/decimal.html#rounding-modes

Last edited 4 months ago by Baptiste Mispelon (previous) (diff)

comment:5 Changed 4 months ago 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.

Note: See TracTickets for help on using tickets.
Back to Top