[Django] #23935: DecimalField in admin can show up as Scientific Notation

201 views
Skip to first unread message

Django

unread,
Nov 30, 2014, 2:59:47 AM11/30/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
-------------------------------+--------------------------------
Reporter: xblitz | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.7
Severity: Normal | Keywords: DecimalField admin
Triage Stage: Unreviewed | Has patch: 1
Easy pickings: 1 | UI/UX: 0
-------------------------------+--------------------------------
In the django admin, if a DecimalField's value is lower than 0.000001
displays as Scientific Notation (ex: 1E-7) since python Decimals converts
automatically to a scientific notation (exponential) ex:
Decimal("0.0000001") = Decimal('1E-7')

this is mostly visible when using Postgresql since DecimalFields with a
value of 0 are saved with the decimal precision higher than 6 , ex:
0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
displayed as 0E-8

the bug can be fixed by converting the Decimal to a fixed point value in
admin/utils.py:

@@ -375,7 +375,7 @@ def display_for_field(value, field):
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
- return formats.number_format(value, field.decimal_places)
+ return formats.number_format(format(value, "f"),
field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
else:

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

Django

unread,
Nov 30, 2014, 3:00:40 AM11/30/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------

Reporter: xblitz | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.7
Severity: Normal | Resolution:

Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

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


Old description:

> In the django admin, if a DecimalField's value is lower than 0.000001
> displays as Scientific Notation (ex: 1E-7) since python Decimals converts
> automatically to a scientific notation (exponential) ex:
> Decimal("0.0000001") = Decimal('1E-7')
>
> this is mostly visible when using Postgresql since DecimalFields with a
> value of 0 are saved with the decimal precision higher than 6 , ex:
> 0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
> displayed as 0E-8
>
> the bug can be fixed by converting the Decimal to a fixed point value in
> admin/utils.py:
>
> @@ -375,7 +375,7 @@ def display_for_field(value, field):
> elif isinstance(field, (models.DateField, models.TimeField)):
> return formats.localize(value)
> elif isinstance(field, models.DecimalField):
> - return formats.number_format(value, field.decimal_places)
> + return formats.number_format(format(value, "f"),
> field.decimal_places)
> elif isinstance(field, models.FloatField):
> return formats.number_format(value)
> else:

New description:

In the django admin, if a DecimalField's value is lower than 0.000001
displays as Scientific Notation (ex: 1E-7) since python Decimals converts
automatically to a scientific notation (exponential) ex:
Decimal("0.0000001") = Decimal('1E-7')

this is mostly visible when using Postgresql since DecimalFields with a
value of 0 are saved with the decimal precision higher than 6 , ex:
0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
displayed as 0E-8

the bug can be fixed by converting the Decimal to a fixed point value in
admin/utils.py:


{{{
@@ -375,7 +375,7 @@ def display_for_field(value, field):
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
- return formats.number_format(value, field.decimal_places)
+ return formats.number_format(format(value, "f"),
field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
else:
}}}

--

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

Django

unread,
Nov 30, 2014, 3:33:37 AM11/30/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------

Reporter: xblitz | Owner: nobody
Type: Bug | Status: new
Component: contrib.admin | Version: 1.7
Severity: Normal | Resolution:

Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by xblitz:

Old description:

> In the django admin, if a DecimalField's value is lower than 0.000001
> displays as Scientific Notation (ex: 1E-7) since python Decimals converts
> automatically to a scientific notation (exponential) ex:
> Decimal("0.0000001") = Decimal('1E-7')
>
> this is mostly visible when using Postgresql since DecimalFields with a
> value of 0 are saved with the decimal precision higher than 6 , ex:
> 0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
> displayed as 0E-8
>
> the bug can be fixed by converting the Decimal to a fixed point value in
> admin/utils.py:
>

> {{{
> @@ -375,7 +375,7 @@ def display_for_field(value, field):
> elif isinstance(field, (models.DateField, models.TimeField)):
> return formats.localize(value)
> elif isinstance(field, models.DecimalField):
> - return formats.number_format(value, field.decimal_places)
> + return formats.number_format(format(value, "f"),
> field.decimal_places)
> elif isinstance(field, models.FloatField):
> return formats.number_format(value)
> else:
> }}}

New description:

In the django admin, if a DecimalField's value is lower than 0.000001
displays as Scientific Notation (ex: 1E-7) since python Decimals converts
automatically to a scientific notation (exponential) ex:
Decimal("0.0000001") = Decimal('1E-7')

this is mostly visible when using Postgresql since DecimalFields with a
value of 0 are saved with the decimal precision higher than 6 , ex:
0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
displayed as 0E-8

the bug can be fixed by converting the Decimal to a fixed point value in
admin/utils.py:


{{{
@@ -375,7 +375,7 @@ def display_for_field(value, field):
elif isinstance(field, (models.DateField, models.TimeField)):
return formats.localize(value)
elif isinstance(field, models.DecimalField):
- return formats.number_format(value, field.decimal_places)
+ return formats.number_format(format(value, "f"),
field.decimal_places)
elif isinstance(field, models.FloatField):
return formats.number_format(value)
else:
}}}

link to my branch: https://github.com/xblitz/django/tree/ticket_23935

--

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

Django

unread,
Dec 1, 2014, 12:45:18 PM12/1/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.7
Severity: Normal | Resolution:

Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Changes (by xblitz):

* status: new => assigned
* owner: nobody => xblitz


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

Django

unread,
Dec 2, 2014, 6:29:14 PM12/2/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: assigned
Component: contrib.admin | Version: 1.7
Severity: Normal | Resolution:

Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
------------------------------------+--------------------------------------
Description changed by xblitz:

Old description:

> In the django admin, if a DecimalField's value is lower than 0.000001


> displays as Scientific Notation (ex: 1E-7) since python Decimals converts
> automatically to a scientific notation (exponential) ex:
> Decimal("0.0000001") = Decimal('1E-7')
>
> this is mostly visible when using Postgresql since DecimalFields with a
> value of 0 are saved with the decimal precision higher than 6 , ex:
> 0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
> displayed as 0E-8
>
> the bug can be fixed by converting the Decimal to a fixed point value in
> admin/utils.py:
>

> {{{
> @@ -375,7 +375,7 @@ def display_for_field(value, field):
> elif isinstance(field, (models.DateField, models.TimeField)):
> return formats.localize(value)
> elif isinstance(field, models.DecimalField):
> - return formats.number_format(value, field.decimal_places)
> + return formats.number_format(format(value, "f"),
> field.decimal_places)
> elif isinstance(field, models.FloatField):
> return formats.number_format(value)
> else:
> }}}
>

New description:

In the django admin, if a DecimalField's value is lower than 0.000001
displays as Scientific Notation (ex: 1E-7) since python Decimals converts
automatically to a scientific notation (exponential) ex:
Decimal("0.0000001") = Decimal('1E-7')

this is mostly visible when using Postgresql since DecimalFields with a
value of 0 are saved with the decimal precision higher than 6 , ex:
0.00000000 then in the admin instead of seeing 0 or 0.00000000 it is
displayed as 0E-8

the bug can be fixed by converting the Decimal to a fixed point value in

django.utils.numberformat.format:

--

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

Django

unread,
Dec 3, 2014, 7:53:12 AM12/3/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
-------------------------------------+-------------------------------------

Reporter: xblitz | Owner: xblitz
Type: Bug | Status: assigned
Component: Utilities | Version: 1.7
Severity: Normal | Resolution:
Keywords: DecimalField admin | Triage Stage: Ready for
Has patch: 1 | checkin
Needs tests: 0 | Needs documentation: 0
Easy pickings: 1 | Patch needs improvement: 0
| UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by timgraham):

* component: contrib.admin => Utilities
* stage: Unreviewed => Ready for checkin


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

Django

unread,
Dec 3, 2014, 7:55:55 AM12/3/14
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
-------------------------------------+-------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: closed
Component: Utilities | Version: 1.7
Severity: Normal | Resolution: fixed

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

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


Comment:

In [changeset:"9d1a69579b996eaa208f141b409141be63f597ec"]:
{{{
#!CommitTicketReference repository=""
revision="9d1a69579b996eaa208f141b409141be63f597ec"
Fixed #23935 -- Converted decimals to fixed point in
utils.numberformat.format
}}}

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

Django

unread,
Jan 14, 2015, 12:08:05 PM1/14/15
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------

Reporter: xblitz | Owner: xblitz
Type: Bug | Status: closed
Component: Utilities | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed

Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0

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

* severity: Normal => Release blocker
* cc: cmawebsite@… (added)
* version: 1.7 => 1.8alpha1
* easy: 1 => 0
* has_patch: 1 => 0
* stage: Ready for checkin => Unreviewed


Comment:

I'm using a custom "Money" subclass of DecimalField that automatically
adds a dollar sign when displaying. It works fine on 1.7 but now it
doesn't work.

Is it too late possibly change this? We could move this to a {{
value|decimalformat }} filter, or we could do {{ |stringformat:"F" }}

If the ship has already sailed, that's ok too. I can always find a
different way to do it.

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

Django

unread,
Jan 14, 2015, 12:17:04 PM1/14/15
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: closed
Component: Utilities | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed
Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by collinanderson):

On 1.7 the template renders the string "$27.98", but on 1.8 its "27.98".

My Money() subclass overrides __str__/__unicode__.

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

Django

unread,
Jan 14, 2015, 12:43:47 PM1/14/15
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: closed
Component: Utilities | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed
Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by timgraham):

I don't see how this low-level fix would translate to using a template
filter (e.g. in form inputs)? Nor do I understand your code well enough to
understand how this fix broke it. If you had a test or even a snippet to
demonstrate the change in behavior, that would be helpful.

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

Django

unread,
Jan 14, 2015, 9:09:53 PM1/14/15
to django-...@googlegroups.com
#23935: DecimalField in admin can show up as Scientific Notation
------------------------------------+--------------------------------------
Reporter: xblitz | Owner: xblitz
Type: Bug | Status: closed
Component: Utilities | Version: 1.8alpha1
Severity: Release blocker | Resolution: fixed
Keywords: DecimalField admin | Triage Stage: Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
------------------------------------+--------------------------------------

Comment (by collinanderson):

Ok, here's a rough failing test case demonstrating my issue:
https://github.com/django/django/pull/3921

--
Ticket URL: <https://code.djangoproject.com/ticket/23935#comment:10>

Reply all
Reply to author
Forward
0 new messages