From 3cd49aeaded6b9d4c9a76fda5078cdae3907c6b0 Mon Sep 17 00:00:00 2001
From: yasondinalt <yason.bad@gmail.com>
Date: Mon, 4 Apr 2016 04:26:55 +0300
Subject: [PATCH] DecimalField rounding: first cast to str, then use
ROUND_HALF_UP
---
django/db/backends/utils.py | 1 +
django/db/models/fields/__init__.py | 2 +-
2 files changed, 2 insertions(+), 1 deletion(-)
diff --git a/django/db/backends/utils.py b/django/db/backends/utils.py
index 9413481..7b865de 100644
a
|
b
|
def format_number(value, max_digits, decimal_places):
|
194 | 194 | return None |
195 | 195 | if isinstance(value, decimal.Decimal): |
196 | 196 | context = decimal.getcontext().copy() |
| 197 | context.rounding = decimal.ROUND_HALF_UP |
197 | 198 | if max_digits is not None: |
198 | 199 | context.prec = max_digits |
199 | 200 | if decimal_places is not None: |
diff --git a/django/db/models/fields/__init__.py b/django/db/models/fields/__init__.py
index 0e42505..beeccd7 100644
a
|
b
|
class DecimalField(Field):
|
1604 | 1604 | if value is None: |
1605 | 1605 | return value |
1606 | 1606 | try: |
1607 | | return decimal.Decimal(value) |
| 1607 | if isinstance(value, six.text_type): |
| 1608 | return decimal.Decimal(value) |
| 1609 | else: |
| 1610 | return decimal.Decimal(str(value)) |
1608 | 1611 | except decimal.InvalidOperation: |
1609 | 1612 | raise exceptions.ValidationError( |
1610 | 1613 | self.error_messages['invalid'], |