Author: kmtracey Date: 2008-11-11 18:35:24 -0600 (Tue, 11 Nov 2008) New Revision: 9394
Modified: django/trunk/django/db/backends/util.py django/trunk/tests/regressiontests/model_fields/models.py Log: Fixed #5079 -- Avoid converting Decimals to floats during save to the database. Modified: django/trunk/django/db/backends/util.py =================================================================== --- django/trunk/django/db/backends/util.py 2008-11-11 17:28:43 UTC (rev 9393) +++ django/trunk/django/db/backends/util.py 2008-11-12 00:35:24 UTC (rev 9394) @@ -124,4 +124,9 @@ Formats a number into a string with the requisite number of digits and decimal places. """ - return u"%.*f" % (decimal_places, value) + if isinstance(value, decimal.Decimal): + context = decimal.getcontext().copy() + context.prec = max_digits + return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context)) + else: + return u"%.*f" % (decimal_places, value) Modified: django/trunk/tests/regressiontests/model_fields/models.py =================================================================== --- django/trunk/tests/regressiontests/model_fields/models.py 2008-11-11 17:28:43 UTC (rev 9393) +++ django/trunk/tests/regressiontests/model_fields/models.py 2008-11-12 00:35:24 UTC (rev 9394) @@ -32,6 +32,9 @@ (0,'Other'), ) c = models.IntegerField(choices=CHOICES, null=True) + +class BigD(models.Model): + d = models.DecimalField(max_digits=38, decimal_places=30) __test__ = {'API_TESTS':""" # Create a couple of Places. @@ -78,5 +81,11 @@ >>> Foo.objects.filter(d=u'1.23') [] - +# Regression test for #5079 -- ensure decimals don't go through a corrupting +# float conversion during save. +>>> bd = BigD(d="12.9") +>>> bd.save() +>>> bd = BigD.objects.get(pk=bd.pk) +>>> bd.d == decimal.Decimal("12.9") +True """} --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django updates" group. To post to this group, send email to django-updates@googlegroups.com 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 -~----------~----~----~----~------~----~------~--~---