Author: kmtracey
Date: 2008-11-11 18:38:48 -0600 (Tue, 11 Nov 2008)
New Revision: 9395

Modified:
   django/branches/releases/1.0.X/
   django/branches/releases/1.0.X/django/db/backends/util.py
   django/branches/releases/1.0.X/tests/regressiontests/model_fields/models.py
Log:
[1.0.X] Fixed #5079 -- Avoid converting Decimals to floats during save to the 
database.

[9394] from trunk.



Property changes on: django/branches/releases/1.0.X
___________________________________________________________________
Name: svnmerge-integrated
   - 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9390
   + 
/django/trunk:1-9097,9099-9102,9104-9109,9111,9113-9144,9146-9151,9153-9156,9158-9159,9161-9187,9189-9247,9249-9262,9264-9277,9279-9298,9301-9302,9305-9331,9333-9343,9345,9347,9350-9352,9355-9394

Modified: django/branches/releases/1.0.X/django/db/backends/util.py
===================================================================
--- django/branches/releases/1.0.X/django/db/backends/util.py   2008-11-12 
00:35:24 UTC (rev 9394)
+++ django/branches/releases/1.0.X/django/db/backends/util.py   2008-11-12 
00:38:48 UTC (rev 9395)
@@ -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/branches/releases/1.0.X/tests/regressiontests/model_fields/models.py
===================================================================
--- django/branches/releases/1.0.X/tests/regressiontests/model_fields/models.py 
2008-11-12 00:35:24 UTC (rev 9394)
+++ django/branches/releases/1.0.X/tests/regressiontests/model_fields/models.py 
2008-11-12 00:38:48 UTC (rev 9395)
@@ -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
-~----------~----~----~----~------~----~------~--~---

Reply via email to