Skip to content

Commit

Permalink
Transform decimal precision getter into a free function.
Browse files Browse the repository at this point in the history
  • Loading branch information
kdeldycke committed Jun 26, 2017
1 parent 22b8f81 commit 8c70916
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,19 @@ def format_number(number, locale=LC_NUMERIC):
return format_decimal(number, locale=locale)


def get_decimal_precision(number):
"""Return maximum precision of a decimal instance's fractional part.
Precision is extracted from the fractional part only.
"""
# Copied from: https://github.com/mahmoud/boltons/pull/59
assert isinstance(number, decimal.Decimal)
decimal_tuple = number.normalize().as_tuple()
if decimal_tuple.exponent >= 0:
return 0
return abs(decimal_tuple.exponent)


def format_decimal(
number, format=None, locale=LC_NUMERIC,
decimal_quantization=True):
Expand Down Expand Up @@ -717,18 +730,6 @@ def compute_scale(self):
scale = 3
return scale

@staticmethod
def precision(number):
"""Return maximum precision of a decimal instance's fractional part.
Precision is extracted from the fractional part only.
"""
# Copied from: https://github.com/mahmoud/boltons/pull/59
decimal_tuple = number.normalize().as_tuple()
if decimal_tuple.exponent >= 0:
return 0
return abs(decimal_tuple.exponent)

@staticmethod
def quantum(precision):
"""Return minimal quantum of a number, as defined by precision."""
Expand Down Expand Up @@ -785,7 +786,7 @@ def apply(
# Bump decimal precision to the natural precision of the number if it
# exceeds the one we're about to use.
if not decimal_quantization:
frac_prec = (frac_prec[0], max([frac_prec[1], self.precision(value)]))
frac_prec = (frac_prec[0], max([frac_prec[1], get_decimal_precision(value)]))

# Render scientific notation.
if self.exp_prec:
Expand Down

0 comments on commit 8c70916

Please sign in to comment.