derek-schaefer / django-json-field

Generic JSON model and form fields.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Decimal rounding

joke2k opened this issue · comments

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?

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.

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.