Commit 4db23cae by Tom Christie

Tweaks to DecimalField

parent 249253a1
...@@ -521,20 +521,21 @@ class DecimalField(Field): ...@@ -521,20 +521,21 @@ class DecimalField(Field):
return value return value
def to_representation(self, value): def to_representation(self, value):
if isinstance(value, decimal.Decimal): if value in (None, ''):
context = decimal.getcontext().copy() return None
context.prec = self.max_digits
quantized = value.quantize(
decimal.Decimal('.1') ** self.decimal_places,
context=context
)
if not self.coerce_to_string:
return quantized
return '{0:f}'.format(quantized)
if not isinstance(value, decimal.Decimal):
value = decimal.Decimal(value)
context = decimal.getcontext().copy()
context.prec = self.max_digits
quantized = value.quantize(
decimal.Decimal('.1') ** self.decimal_places,
context=context
)
if not self.coerce_to_string: if not self.coerce_to_string:
return value return quantized
return '%.*f' % (self.max_decimal_places, value) return '{0:f}'.format(quantized)
# Date & time fields... # Date & time fields...
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment