Fixed #5079 -- Avoid converting Decimals to floats during save to the database.
git-svn-id: http://code.djangoproject.com/svn/django/trunk@9394 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
parent
1142dd430f
commit
cb6a5886f6
|
@ -124,4 +124,9 @@ def format_number(value, max_digits, decimal_places):
|
||||||
Formats a number into a string with the requisite number of digits and
|
Formats a number into a string with the requisite number of digits and
|
||||||
decimal places.
|
decimal places.
|
||||||
"""
|
"""
|
||||||
|
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)
|
return u"%.*f" % (decimal_places, value)
|
||||||
|
|
|
@ -33,6 +33,9 @@ class Whiz(models.Model):
|
||||||
)
|
)
|
||||||
c = models.IntegerField(choices=CHOICES, null=True)
|
c = models.IntegerField(choices=CHOICES, null=True)
|
||||||
|
|
||||||
|
class BigD(models.Model):
|
||||||
|
d = models.DecimalField(max_digits=38, decimal_places=30)
|
||||||
|
|
||||||
__test__ = {'API_TESTS':"""
|
__test__ = {'API_TESTS':"""
|
||||||
# Create a couple of Places.
|
# Create a couple of Places.
|
||||||
>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
|
>>> f = Foo.objects.create(a='abc', d=decimal.Decimal("12.34"))
|
||||||
|
@ -78,5 +81,11 @@ u''
|
||||||
>>> Foo.objects.filter(d=u'1.23')
|
>>> 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
|
||||||
"""}
|
"""}
|
||||||
|
|
Loading…
Reference in New Issue