Skip to content

Commit 6238f66

Browse files
committed
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
1 parent 8ebdac3 commit 6238f66

2 files changed

Lines changed: 5 additions & 9 deletions

File tree

babel/_compat.py

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -61,16 +61,12 @@
6161
# Use cdecimal when available
6262
#
6363
from decimal import (Decimal as _dec,
64-
InvalidOperation as _invop,
65-
ROUND_HALF_EVEN as _RHE)
64+
InvalidOperation as _invop)
6665
try:
6766
from cdecimal import (Decimal as _cdec,
68-
InvalidOperation as _cinvop,
69-
ROUND_HALF_EVEN as _CRHE)
67+
InvalidOperation as _cinvop)
7068
Decimal = _cdec
7169
InvalidOperation = (_invop, _cinvop)
72-
ROUND_HALF_EVEN = _CRHE
7370
except ImportError:
7471
Decimal = _dec
7572
InvalidOperation = _invop
76-
ROUND_HALF_EVEN = _RHE

babel/numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
from datetime import date as date_, datetime as datetime_
2323

2424
from babel.core import default_locale, Locale, get_global
25-
from babel._compat import Decimal, InvalidOperation, ROUND_HALF_EVEN
25+
from babel._compat import Decimal, InvalidOperation
2626

2727

2828
LC_NUMERIC = default_locale('LC_NUMERIC')
@@ -604,7 +604,7 @@ def apply(self, value, locale, currency=None, force_frac=None):
604604
number += get_decimal_symbol(locale) + b
605605
else: # A normal number pattern
606606
precision = Decimal('1.' + '1' * frac_prec[1])
607-
rounded = value.quantize(precision, ROUND_HALF_EVEN)
607+
rounded = value.quantize(precision)
608608
a, sep, b = str(abs(rounded)).partition(".")
609609
number = (self._format_int(a, self.int_prec[0],
610610
self.int_prec[1], locale) +
@@ -641,7 +641,7 @@ def apply(self, value, locale, currency=None, force_frac=None):
641641
def _format_significant(self, value, minimum, maximum):
642642
exp = value.adjusted()
643643
scale = maximum - 1 - exp
644-
digits = str(value.scaleb(scale).quantize(Decimal(1), ROUND_HALF_EVEN))
644+
digits = str(value.scaleb(scale).quantize(Decimal(1)))
645645
if scale <= 0:
646646
result = digits + '0' * -scale
647647
else:

0 commit comments

Comments
 (0)