derek-schaefer / django-json-field

Generic JSON model and form fields.
BSD 3-Clause "New" or "Revised" License
122 stars 86 forks source link

Decimal rounding #14

Closed joke2k closed 12 years ago

joke2k commented 12 years ago

Hello, i was comparing your django-json-field with django-jsonfield and the one included in django-extensions, and your is the best for my needs (write a real generic value... not a dict or list of generic values).

but running you test cases, i noticed that test_decimal() FAIL, reporting:

self.assertEqual(Decimal(1.24), self.json_model.objects.get(pk=t1.pk).json)
AssertionError: Decimal('1.2399999999999999911182158029987476766109466552734375') != u'1.2399999999999999911182158029987476766109466552734375'

what we can do for that?

derek-schaefer commented 12 years ago

Interesting, thanks for the report. I'll start looking into how to correct this. What is your environment like? Python and Django versions, OS, etc.?

This issue seems related to the current state of Decimal deserialization, which I've wanted to address for a while. Decimals are serialized with their full precision but are deserialized to floats which can cause loss of that information. The solution is to figure out a way to deduce how a floating-point number should be deserialized based on it's precision.

derek-schaefer commented 12 years ago

On second thought, I am going to play it safe and deserialize all floats as Decimal. From there it is left to the user to perform any conversions as necessary.