Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

[1.0.X] Fixed #5079 -- Avoid converting Decimals to floats during sav…

…e to the database.

[9394] from trunk.


git-svn-id: http://code.djangoproject.com/svn/django/branches/releases/1.0.X@9395 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit 2a1c9072fb7db5085c817912ed8df6c48ed0bb7e 1 parent 1173698
Karen Tracey authored
7  django/db/backends/util.py
@@ -124,4 +124,9 @@ def format_number(value, max_digits, decimal_places):
124 124
     Formats a number into a string with the requisite number of digits and
125 125
     decimal places.
126 126
     """
127  
-    return u"%.*f" % (decimal_places, value)
  127
+    if isinstance(value, decimal.Decimal):
  128
+        context = decimal.getcontext().copy()
  129
+        context.prec = max_digits
  130
+        return u'%s' % str(value.quantize(decimal.Decimal(".1") ** decimal_places, context=context))
  131
+    else:
  132
+        return u"%.*f" % (decimal_places, value)
11  tests/regressiontests/model_fields/models.py
@@ -32,6 +32,9 @@ class Whiz(models.Model):
32 32
         (0,'Other'),
33 33
     )
34 34
     c = models.IntegerField(choices=CHOICES, null=True)
  35
+    
  36
+class BigD(models.Model):
  37
+    d = models.DecimalField(max_digits=38, decimal_places=30)
35 38
 
36 39
 __test__ = {'API_TESTS':"""
37 40
 # Create a couple of Places.
@@ -78,5 +81,11 @@ class Whiz(models.Model):
78 81
 >>> Foo.objects.filter(d=u'1.23')
79 82
 []
80 83
 
81  
-
  84
+# Regression test for #5079 -- ensure decimals don't go through a corrupting
  85
+# float conversion during save.  
  86
+>>> bd = BigD(d="12.9")
  87
+>>> bd.save()
  88
+>>> bd = BigD.objects.get(pk=bd.pk)
  89
+>>> bd.d == decimal.Decimal("12.9")
  90
+True
82 91
 """}

0 notes on commit 2a1c907

Please sign in to comment.
Something went wrong with that request. Please try again.