In a project I am working on, we need to store monetary values in the database, and we want to avoid floats altogether so that we don't lose precision or have representation errors. The existing FloatField was not suitable for use, because it would sometimes return floats and sometimes decimals, depending on circumstances. I submitted a ticket & patch, #2365 (http://code.djangoproject.com/ticket/2365) that addresses this by making FloatField work exclusively with floats, and added DecimalField (in db. and forms.) that would work with decimal.Decimal objects. The problem with the latter is that the decimal module is only in 2.4, and django is targeting 2.3 as the minimum support version of Python.
To quote from what I wrote in the ticket: > Finally, this patch should work without the decimal module: if the > decimal module is available, then models.DecimalField? attributes will > be decimal.Decimal instances; if not, then they will be strings. If the > user needs to perform arithmetic, then he can call float() and operate > within the accuracy limits of floats, but it's safer not to convert > implicitly. To which Malcom replied: > I don't like the implication of the last paragraph of the previous > comment. It means that every single time you do arithmetic with this > field, you need to either test that you are running on Python 2.4 or > call float(). This is a huge burden on the developer. It has to work a > bit more transparently than that. So, which is better to do if the decimal module is not available: is it better to return a string from a DecimalField, sacrificing convenience for a guarantee of exact representation, or return a float so that the user can do arithmetic without hassle? I'm of the opinion that the former is better, as it requires the developer to explicitly take actions that may result in a loss of information; and for those cases where the developer either needs floats or doesn't care as much about the accuracy, he can use models.FloatField, which will consistently return floats. What do others think? Andrew --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django developers" 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-developers -~----------~----~----~----~------~----~------~--~---
