Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DecimalField Converts Zero to 0E-10

Tags:

Django 1.6, Python 2.7.5+, PostgreSQL 9.1, Linux.

I have several decimal fields defined in the model like this:

min_inclusive = models.DecimalField(_('minimum inclusive'), max_digits=19,
                    decimal_places=10,
                    help_text=_("Enter the minimum (inclusive) value for this concept."),
                    null=True, blank=True)

Via the admin interface when I enter a 0 (zero) and save the object I get 0E-10 as the value.

So, why doesn't it just store and display a zero?

The documentation doesn't specify if decimal_places is forced or if it is a maximum number.

Is this display because 10 places are required? If so, the using the default form widget, TextInput, it seems to me that is should just expand to that size or at least scroll to that size.

In PostgreSQL it is stored as 0.0000000000

like image 549
Tim Cook Avatar asked Jan 08 '14 15:01

Tim Cook


People also ask

What is the value of 0E 10?

So if you insert 0 in an SQLite number field and then select it, you will get 0 . But if you insert 0 in a PostgreSQL numeric field it will do a zero fill on the decimal and insert 0.0000000000 . So when Python puts this in a Decimal you will see Decimal("0E-10") .

What does 0E 8 mean?

1 - decimal value 0.00000000 converted to scientific notation 0E-8.


1 Answers

I have also ran into this problem and found out that actually everything is working right. In Python if you actually define something like this Decimal("0.0000000000") you will see this in the console:

>>> Decimal("0.0000000000")
Decimal('0E-10')

In my situation I was converting from SQLite3 backend to PostgreSQL for production. Everything was working fine in SQLite, because it doesn't explicitly show (or store) all decimal digits. So if you insert 0 in an SQLite number field and then select it, you will get 0.

But if you insert 0 in a PostgreSQL numeric field it will do a zero fill on the decimal and insert 0.0000000000. So when Python puts this in a Decimal you will see Decimal("0E-10").

UPDATE - fixed in Django 1.8

like image 160
xblitz Avatar answered Sep 19 '22 08:09

xblitz