Commits

adamv committed 92e5642

Per issue 38 updated the Decimal handling code to format without an exponent, using "quantize".

Comments (0)

Files changed (2)

source/sqlserver_ado/dbapi.py

 
     if show_value:
         desc = [
-            "Name: %s, Dir.: %s, Type: %s, Size: %s, Value: \"%s\"" %\
-            (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size, p.Value)
+            "Name: %s, Dir.: %s, Type: %s, Size: %s, Value: \"%s\", Precision: %s, NumericScale: %s" %\
+            (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size, p.Value, p.Precision, p.NumericScale)
             for p in parameters ]
     else:
         desc = [
-            "Name: %s, Dir.: %s, Type: %s, Size: %s" %\
-            (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size)
+            "Name: %s, Dir.: %s, Type: %s, Size: %s, Precision: %s, NumericScale: %s" %\
+            (p.Name, directions[p.Direction], adTypeNames.get(p.Type, str(p.Type)+' (unknown type)'), p.Size, p.Precision, p.NumericScale)
             for p in parameters ]
 
     return '[' + ', '.join(desc) + ']'
         p.AppendChunk(value)
 
     elif isinstance(value, decimal.Decimal):
-        s = str(value.normalize())
-        p.Value = value
-        p.Precision = len(s)
+        s = str(value.quantize(decimal.Decimal('0')))
 
         point = s.find('.')
         if point == -1:
         else:
             p.NumericScale = len(s)-point
 
+        p.Precision = len(s)
+        p.Value = value
+
     else:
         # For any other type, set the value and let pythoncom do the right thing.
         p.Value = value

tests/test_main/regressiontests/models.py

             a2.delete()
         except Exception, e:
             self.failUnless(isinstance(e, IntegrityError), 'Expected IntegrityError but got: %s' % type(e))
+
+class Bug38Table(models.Model):
+    """
+    Test adding decimals as strings with various formats.
+    
+    >>> Bug38Table(d=decimal.Decimal('0')).save()
+    >>> Bug38Table(d=decimal.Decimal('0e0')).save()
+    >>> Bug38Table(d=decimal.Decimal('0E0')).save()
+    >>> Bug38Table(d=decimal.Decimal('450')).save()
+    >>> Bug38Table(d=decimal.Decimal('450.0')).save()
+    >>> Bug38Table(d=decimal.Decimal('450.00')).save()
+    >>> Bug38Table(d=decimal.Decimal('450.000')).save()
+    >>> Bug38Table(d=decimal.Decimal('0450')).save()
+    >>> Bug38Table(d=decimal.Decimal('0450.0')).save()
+    >>> Bug38Table(d=decimal.Decimal('0450.00')).save()
+    >>> Bug38Table(d=decimal.Decimal('0450.000')).save()
+    >>> Bug38Table(d=decimal.Decimal('4.5e+2')).save()
+    >>> Bug38Table(d=decimal.Decimal('4.5E+2')).save()
+    >>> len(list(Bug38Table.objects.all()))
+    13
+    """
+    d = models.DecimalField(max_digits=5, decimal_places=2)