[Django] #26459: DecimalField float rounding

12 views
Skip to first unread message

Django

unread,
Apr 4, 2016, 1:58:42 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
----------------------------------------------+--------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
DecimalField has weird behavior in case of float values passed to field.
If decimal_places = 1
float 2.15 will be saved as 2.1 (!)
float 2.25 will be saved as 2.2 (!)
But if float value first converted to str:
float 2.15 will be saved as 0.2 (ok)
float 2.25 will be saved as 0.2 (!)
It's because of default decimal rounding ROUND_HALF_EVEN.
As I understand build-in round() use rounding similar to ROUND_HALF_UP.

I tried first cast float to str, then use ROUND_HALF_UP, so now:
float 2.15 will be saved as 0.2 (ok)
float 2.25 will be saved as 0.3 (ok)

--
Ticket URL: <https://code.djangoproject.com/ticket/26459>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Apr 4, 2016, 1:58:59 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
------------------------------------------+----------------------------

Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+----------------------------
Changes (by yasondinalt):

* Attachment "0001-tests-format-float-to-Decimal-with-proper-
rounding.patch" added.

Tests

Django

unread,
Apr 4, 2016, 1:59:29 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
------------------------------------------+----------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.9
Severity: Normal | Resolution:

Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Easy pickings: 0
UI/UX: 0 |
------------------------------------------+----------------------------
Changes (by yasondinalt):

* Attachment "0001-DecimalField-rounding-first-cast-to-str-then-use-
ROU.patch" added.

Changes

Django

unread,
Apr 4, 2016, 2:07:53 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------

Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

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

* needs_better_patch: => 0
* needs_tests: => 1
* needs_docs: => 0


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

Django

unread,
Apr 4, 2016, 2:08:55 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by yasondinalt):

* Attachment "0001-tests-format-float-to-Decimal-with-proper-
rounding.patch" added.

Tests

--

Django

unread,
Apr 4, 2016, 8:05:53 PM4/4/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by timgraham):

Could you give a snippet of the application code where you run into this
issue?

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

Django

unread,
Apr 5, 2016, 4:23:54 AM4/5/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by yasondinalt):

Replying to [comment:2 timgraham]:


> Could you give a snippet of the application code where you run into this
issue?


{{{
class Invoice(models.Model):
# same as django-paypal use
mc_gross = models.DecimalField(max_digits=64, decimal_places=2,
default=0)

invoice = 10.215 # float
invoice.save()
}}}

In DB will be value 10.21 (or if 10.225 -> 10.22)

--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:3>

Django

unread,
Apr 5, 2016, 4:24:52 AM4/5/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by yasondinalt):

* Attachment "0001-DecimalField-rounding-first-cast-to-str-then-use-
ROU.patch" added.

Changes

Django

unread,
Apr 5, 2016, 9:55:38 AM4/5/16
to django-...@googlegroups.com
#26459: DecimalField float rounding
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.9
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed

Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* needs_tests: 1 => 0


Comment:

I'm not sure whether or not to accept the ticket so I asked for advice on
the [https://groups.google.com/d/topic/django-
developers/bnoVTOx2GFs/discussion django-developers mailing list].

p.s. You don't need to attach patches on the ticket when you also send a
[https://github.com/django/django/pull/6410 pull request].

--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:4>

Django

unread,
Apr 5, 2016, 11:56:10 AM4/5/16
to django-...@googlegroups.com
#26459: Allow providing DecimalField with a custom context
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody
Type: New feature | Status: new

Component: Database layer | Version: 1.9
(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 timgraham):

* has_patch: 1 => 0
* stage: Unreviewed => Accepted
* type: Bug => New feature


Comment:

Accepting per Aymeric's proposal on the mailing list:

Currently `DecimalField` accepts `max_digits` and `decimal_places`
options. I think it should accept a decimal context and delegate all
operations to that context.

I suggest the following behavior:
- start with the decimal context provided in a kwarg to `DecimalField` or,
if there is None, the current context returned by `getcontext()`.
- modify that context to take into account `max_digits` and
`decimal_places`
- ask the context to perform whatever operations we need

--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:5>

Django

unread,
Aug 4, 2020, 1:43:39 AM8/4/20
to django-...@googlegroups.com
#26459: Allow providing DecimalField with a custom context
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(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):

#31841 was marked as a duplicate.

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

Django

unread,
Mar 16, 2021, 7:09:57 AM3/16/21
to django-...@googlegroups.com
#26459: Allow providing DecimalField with a custom context
-------------------------------------+-------------------------------------
Reporter: yasondinalt | Owner: nobody

Type: New feature | Status: new
Component: Database layer | Version: 1.9
(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 Mariusz Felisiak):

#32555 was marked as a duplicate for `ArrayField(DecimalField())`.

--
Ticket URL: <https://code.djangoproject.com/ticket/26459#comment:7>

Reply all
Reply to author
Forward
0 new messages