Den 08/01/2014 kl. 16.18 skrev Timothy W. Cook <t...@mlhim.org>:
> So, why doesn't it just store a zero?Underneath a DecimalField there is a Python float type, and some equivalent float type in your database. Float values are an approximation, so 0.000000000000 and 0 are not guaranteed to be equal. See http://stackoverflow.com/questions/2986150/python-floating-number
> In reality I need to allow the range of, positive infinity to negative infinity. But this doesn't seem possible. Is it?It’s not physically or computationally possible. How will you store an infinite number of digits on a hard drive of finite size? :-)
Den 08/01/2014 kl. 17.11 skrev Timothy W. Cook <t...@mlhim.org>:
> But one would think that if Django calls it a decimal field, it would convert the float to decimal.
> I suppose I'll do that before writing it out to the file (an XML schema) so it really isn't a big deal, just surprising.
Ah, the “decimal” in DecimalField only refers to how you are supposed to enter the values in a form. Python itself only has float and that’s what Django uses internally. Decimal vs. scientific notation is just a matter of how you choose to format the output when converting the float to a string. The default in Python depends on the value:
>>> str(0.1)
'0.1'
>>> str(0.00001)
'1e-05’
No, no, no. None of this is true.Decimals are not a differently-formatted version of floats. Decimals are not a built-in datatype in Python, it's true, but they are provided in the standard library in (not surprisingly) the `decimal` module. As the documentation for that module explains, decimals do provide precise floating-point arithmetic, and are therefore suitable for representing things like currencies. Databases usually also provide a similar datatype, which Django's DecimalField maps to.
On Wed, Jan 8, 2014 at 6:07 PM, Erik Cederstrand <erik+...@cederstrand.dk> wrote:
Den 08/01/2014 kl. 17.56 skrev Daniel Roseman <dan...@roseman.org.uk>:
>
But to post the solution to the issue, just for completeness.Yes, Django does store the field contents as decimal.Decimal objects. In your templates or if writing out to a file (as I need to do) you need to use the formatting option.Example using the previously used model field:str('%.10g' % self.min_inclusive).strip()This removes all non-significant zeros. So it gives me '0' for a zero.
If Django's "DecimalField" is NOT use Python's Decimal type, I'd be
concerned... Python's Decimal is NOT a "float".
However, the conversion of a Decimal to/from PostgreSQL might result in
a floating point value -- which I'd consider a flaw in the database adapter
as PostgreSQL does have a NUMERIC data type that should map directly to
Python Decimal.
--