[Django] #34098: Filter floatformat drops precision in decimal numbers

17 views
Skip to first unread message

Django

unread,
Oct 14, 2022, 8:25:22 AM10/14/22
to django-...@googlegroups.com
#34098: Filter floatformat drops precision in decimal numbers
-------------------------------------------+------------------------
Reporter: Vlastimil Zíma | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: dev
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 1
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------------+------------------------
I discovered that `floatformat` template filter may drop precision when
used for `Decimal` numbers.

MWE:
{{{
#!python
from decimal import Decimal

from django import setup
from django.conf import settings
from django.template import Template, Context

TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
},
]
settings.configure(TEMPLATES=TEMPLATES)
setup()

t = Template('{{ value|floatformat:20 }}')
c = Context({'value': Decimal('42.12345678901234567890')})
print(t.render(c)) #>>> 42.12345678901234400000
}}}

I traced the bug to incorrect conversion to `Decimal` within the
`floatformat` implementation that can't really work for `Decimal` numbers.
`Decimal` numbers are converted to float instead.

Pull request is prepared https://github.com/django/django/pull/15863

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

Django

unread,
Oct 14, 2022, 3:30:37 PM10/14/22
to django-...@googlegroups.com
#34098: Filter floatformat drops precision in decimal numbers
---------------------------------+------------------------------------

Reporter: Vlastimil Zíma | Owner: nobody
Type: Bug | Status: new
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted

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

* stage: Unreviewed => Accepted


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

Django

unread,
Oct 17, 2022, 1:57:55 AM10/17/22
to django-...@googlegroups.com
#34098: Filter floatformat drops precision in decimal numbers
-------------------------------------+-------------------------------------
Reporter: Vlastimil Zíma | Owner: Vlastimil
| Zíma
Type: Bug | Status: assigned

Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Vlastimil Zíma):

* owner: nobody => Vlastimil Zíma
* status: new => assigned


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

Django

unread,
Oct 24, 2022, 6:51:55 AM10/24/22
to django-...@googlegroups.com
#34098: Filter floatformat drops precision in decimal numbers
-------------------------------------+-------------------------------------
Reporter: Vlastimil Zíma | Owner: Vlastimil
| Zíma
Type: Bug | Status: assigned
Component: Template system | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin

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

* stage: Accepted => Ready for checkin


Comment:

Regression in 12f7928f5a455e330c0a7f19bc86b37baca12811.

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

Django

unread,
Oct 24, 2022, 6:59:59 AM10/24/22
to django-...@googlegroups.com
#34098: Filter floatformat drops precision in decimal numbers
-------------------------------------+-------------------------------------
Reporter: Vlastimil Zíma | Owner: Vlastimil
| Zíma
Type: Bug | Status: closed

Component: Template system | Version: dev
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by GitHub <noreply@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"08c5a787262c1ae57f6517d4574b54a5fcaad124" 08c5a787]:
{{{
#!CommitTicketReference repository=""
revision="08c5a787262c1ae57f6517d4574b54a5fcaad124"
Fixed #34098 -- Fixed loss of precision for Decimal values in floatformat
filter.

Regression in 12f7928f5a455e330c0a7f19bc86b37baca12811.
}}}

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

Reply all
Reply to author
Forward
0 new messages