postgresql DecimalField 将零转换为 0E-10

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21000061/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 01:15:36  来源:igfitidea点击:

DecimalField Converts Zero to 0E-10

pythondjangopostgresql

提问by Tim Cook

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

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-10as the value.

当我输入0(零)并将我获得的对象保存0E-10为值时,通过管理界面。

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

那么,为什么它不只存储和显示零呢?

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

该文档没有指定decimal_places是强制还是最大数量。

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.

这个显示是因为需要10 个位置吗?如果是这样,使用默认表单小部件,TextInput在我看来应该只是扩展到该大小或至少滚动到该大小。

In PostgreSQL it is stored as 0.0000000000

在 PostgreSQL 中,它存储为 0.0000000000

采纳答案by xblitz

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:

我也遇到了这个问题,发现实际上一切正常。在 Python 中,如果你真的定义了这样的东西,Decimal("0.0000000000")你会在控制台中看到:

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

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

在我的情况下,我正在从SQLite3后端转换为PostgreSQL进行生产。在 SQLite 中一切正常,因为它没有明确显示(或存储)所有十进制数字。因此,如果您插入0SQLite 数字字段然后选择它,您将获得0.

But if you insert 0in a PostgreSQLnumeric 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").

但是,如果你插入0一个PostgreSQL的数字字段会做一个零填补小数和插入0.0000000000。因此,当 Python 将其放入 Decimal 时,您将看到Decimal("0E-10").

UPDATE - fixed in Django 1.8

更新 - 在 Django 1.8 中修复