Ticket #5079: 5079.3.diff

File 5079.3.diff, 1.9 KB (added by Philippe Raoult, 17 years ago)

fix included now

  • django/db/models/fields/__init__.py

     
    686686        if value < 0:
    687687            num_chars += 1
    688688
    689         return u"%.*f" % (self.decimal_places, value)
     689        if isinstance(value, decimal.Decimal):
     690            return value.quantize(decimal.Decimal('.1')**self.decimal_places)
     691        else:
     692            return u"%.*f" % (self.decimal_places, value)
    690693
    691694    def get_db_prep_save(self, value):
    692695        if value is not None:
  • tests/regressiontests/model_fields/tests.py

     
    1 """
     1
     2
     3from django.db import models
     4from decimal import Decimal
     5
     6class DecimalTest(models.Model):
     7    field = models.DecimalField(max_digits=40, decimal_places=25)
     8   
     9
     10__test__ = {'API_TESTS': """
    211>>> from django.db.models.fields import *
    312
    413# DecimalField
     
    1524Traceback (most recent call last):
    1625...
    1726ValidationError: [u'This value must be a decimal number.']
    18 """
     27
     28# added after #5079
     29>>> b1 = DecimalTest(field=Decimal(".1"))
     30>>> b1.field
     31Decimal("0.1")
     32>>> b1.save()
     33>>> b1.field
     34Decimal("0.1")
     35
     36# had to strip because mysql would return decimals with lots of 0000 at the end
     37>>> str(DecimalTest.objects.get(id=1).field).rstrip('0')
     38'0.1'
     39
     40>>> b1 = DecimalTest(field=Decimal(".10000000000000000000000000000000005"))
     41>>> b1.field
     42Decimal("0.10000000000000000000000000000000005")
     43>>> b1.save()
     44>>> b1.field
     45Decimal("0.10000000000000000000000000000000005")
     46
     47# had to strip because mysql would return decimals with lots of 0000 at the end
     48>>> str(DecimalTest.objects.get(id=2).field).rstrip('0')
     49'0.1'
     50"""}
Back to Top