Ticket #26459: 0001-DecimalField-rounding-first-cast-to-str-then-use-ROU.patch

File 0001-DecimalField-rounding-first-cast-to-str-then-use-ROU.patch, 1.5 KB (added by yasondinalt, 9 years ago)

Changes

  • django/db/backends/utils.py

    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):  
    194194        return None
    195195    if isinstance(value, decimal.Decimal):
    196196        context = decimal.getcontext().copy()
     197        context.rounding = decimal.ROUND_HALF_UP
    197198        if max_digits is not None:
    198199            context.prec = max_digits
    199200        if decimal_places is not None:
  • django/db/models/fields/__init__.py

    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):  
    16041604        if value is None:
    16051605            return value
    16061606        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))
    16081611        except decimal.InvalidOperation:
    16091612            raise exceptions.ValidationError(
    16101613                self.error_messages['invalid'],
Back to Top