Fixed #31487 -- Added precision argument to Round().

This commit is contained in:
Nick Pope 2021-03-24 22:29:33 +00:00 committed by Mariusz Felisiak
parent 61d5e57353
commit 2f13c476ab
7 changed files with 115 additions and 10 deletions

View file

@ -1147,18 +1147,19 @@ Returns a random value in the range ``0.0 ≤ x < 1.0``.
``Round``
---------
.. class:: Round(expression, **extra)
.. class:: Round(expression, precision=0, **extra)
Rounds a numeric field or expression to the nearest integer. Whether half
Rounds a numeric field or expression to ``precision`` (must be an integer)
decimal places. By default, it rounds to the nearest integer. Whether half
values are rounded up or down depends on the database.
Usage example::
>>> from django.db.models.functions import Round
>>> Vector.objects.create(x=5.4, y=-2.3)
>>> vector = Vector.objects.annotate(x_r=Round('x'), y_r=Round('y')).get()
>>> Vector.objects.create(x=5.4, y=-2.37)
>>> vector = Vector.objects.annotate(x_r=Round('x'), y_r=Round('y', precision=1)).get()
>>> vector.x_r, vector.y_r
(5.0, -2.0)
(5.0, -2.4)
It can also be registered as a transform. For example::
@ -1168,6 +1169,10 @@ It can also be registered as a transform. For example::
>>> # Get vectors whose round() is less than 20
>>> vectors = Vector.objects.filter(x__round__lt=20, y__round__lt=20)
.. versionchanged:: 4.0
The ``precision`` argument was added.
``Sign``
--------

View file

@ -222,6 +222,10 @@ Models
whether the queryset contains the given object. This tries to perform the
query in the simplest and fastest way possible.
* The new ``precision`` argument of the
:class:`Round() <django.db.models.functions.Round>` database function allows
specifying the number of decimal places after rounding.
Requests and Responses
~~~~~~~~~~~~~~~~~~~~~~