#14659: Assigning floats to DecimalFields
---------------------------------------------------+------------------------
Reporter: maraujop | Owner: maraujop
Status: closed | Milestone: 1.3
Component: Database layer (models, ORM) | Version: SVN
Resolution: invalid | Keywords:
DecimalField, float
Stage: Unreviewed | Has_patch: 1
Needs_docs: 0 | Needs_tests: 0
Needs_better_patch: 0 |
---------------------------------------------------+------------------------
Changes (by dmoisset):
* status: new => closed
* needs_better_patch: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_docs: => 0
Comment:
This follows the same rule as the stdlib Decimal class:
{{{
>>> from decimal import Decimal
>>> Decimal(100.43)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/lib/python2.6/decimal.py", line 649, in __new__
"First convert the float to a string")
TypeError: Cannot convert float to Decimal. First convert the float to a
string
}}}
This rule is explicitly there to avoid nasty and hard to debug bugs: the
100.43 you write in the code is not actually 100.43 (but a very close
float value), so you could end up passing a value which is not the one the
code leads you to believe. So enforcing the conversion to be explicit is
safer.
--
Ticket URL: <http://code.djangoproject.com/ticket/14659#comment:1>
Django <http://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/django-updates?hl=en.