From: Isaac Jurado Date: Sat, 28 May 2016 21:00:24 +0000 (+0200) Subject: Remove explicit enforcement from Decimal rounding X-Git-Tag: v2.4.0~16^2~2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=6238f6620c54d0343e923ef92130b35f2bd117d0;p=thirdparty%2Fbabel.git Remove explicit enforcement from Decimal rounding Allow the rounding mode to be controlled from the currently active decimal context. This gives the caller the ability to control rounding mode, precision, exponent range and all attributes that affect decimal operations. Fixes #90 --- diff --git a/babel/_compat.py b/babel/_compat.py index 75abf9eb..ae290f42 100644 --- a/babel/_compat.py +++ b/babel/_compat.py @@ -61,16 +61,12 @@ number_types = integer_types + (float,) # Use cdecimal when available # from decimal import (Decimal as _dec, - InvalidOperation as _invop, - ROUND_HALF_EVEN as _RHE) + InvalidOperation as _invop) try: from cdecimal import (Decimal as _cdec, - InvalidOperation as _cinvop, - ROUND_HALF_EVEN as _CRHE) + InvalidOperation as _cinvop) Decimal = _cdec InvalidOperation = (_invop, _cinvop) - ROUND_HALF_EVEN = _CRHE except ImportError: Decimal = _dec InvalidOperation = _invop - ROUND_HALF_EVEN = _RHE diff --git a/babel/numbers.py b/babel/numbers.py index 3ab366cc..67881ae5 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -22,7 +22,7 @@ import re from datetime import date as date_, datetime as datetime_ from babel.core import default_locale, Locale, get_global -from babel._compat import Decimal, InvalidOperation, ROUND_HALF_EVEN +from babel._compat import Decimal, InvalidOperation LC_NUMERIC = default_locale('LC_NUMERIC') @@ -604,7 +604,7 @@ class NumberPattern(object): number += get_decimal_symbol(locale) + b else: # A normal number pattern precision = Decimal('1.' + '1' * frac_prec[1]) - rounded = value.quantize(precision, ROUND_HALF_EVEN) + rounded = value.quantize(precision) a, sep, b = str(abs(rounded)).partition(".") number = (self._format_int(a, self.int_prec[0], self.int_prec[1], locale) + @@ -641,7 +641,7 @@ class NumberPattern(object): def _format_significant(self, value, minimum, maximum): exp = value.adjusted() scale = maximum - 1 - exp - digits = str(value.scaleb(scale).quantize(Decimal(1), ROUND_HALF_EVEN)) + digits = str(value.scaleb(scale).quantize(Decimal(1))) if scale <= 0: result = digits + '0' * -scale else: