Skip to content

Commit dba70a5

Browse files
committed
Transform decimal quantum getter into a free function.
1 parent 8c70916 commit dba70a5

1 file changed

Lines changed: 14 additions & 3 deletions

File tree

babel/numbers.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@
2828
from babel._compat import decimal, string_types
2929
from babel.localedata import locale_identifiers
3030

31+
try:
32+
long
33+
except NameError:
34+
long = int
35+
3136

3237
LC_NUMERIC = default_locale('LC_NUMERIC')
3338

@@ -332,6 +337,12 @@ def get_decimal_precision(number):
332337
return abs(decimal_tuple.exponent)
333338

334339

340+
def get_decimal_quantum(precision):
341+
"""Return minimal quantum of a number, as defined by precision."""
342+
assert isinstance(precision, (int, long, decimal.Decimal))
343+
return decimal.Decimal(10) ** (-precision)
344+
345+
335346
def format_decimal(
336347
number, format=None, locale=LC_NUMERIC,
337348
decimal_quantization=True):
@@ -757,15 +768,15 @@ def apply(
757768

758769
# Normalize value to only have one lead digit.
759770
exp = value.adjusted()
760-
value = value * self.quantum(exp)
771+
value = value * get_decimal_quantum(exp)
761772
assert value.adjusted() == 0
762773

763774
# Shift exponent and value by the minimum number of leading digits
764775
# imposed by the rendering pattern. And always make that number
765776
# greater or equal to 1.
766777
lead_shift = max([1, min(self.int_prec)]) - 1
767778
exp = exp - lead_shift
768-
value = value * self.quantum(-lead_shift)
779+
value = value * get_decimal_quantum(-lead_shift)
769780

770781
# Get exponent sign symbol.
771782
exp_sign = ''
@@ -879,7 +890,7 @@ def _format_int(self, value, min, max, locale):
879890
return value + ret
880891

881892
def _quantize_value(self, value, locale, frac_prec):
882-
quantum = self.quantum(frac_prec[1])
893+
quantum = get_decimal_quantum(frac_prec[1])
883894
rounded = value.quantize(quantum)
884895
a, sep, b = str(rounded).partition(".")
885896
number = (self._format_int(a, self.int_prec[0],

0 commit comments

Comments
 (0)