From 8c70916af3144e9d435a26736bea1f7c3efeb0a0 Mon Sep 17 00:00:00 2001 From: Kevin Deldycke Date: Mon, 26 Jun 2017 15:32:11 +0200 Subject: [PATCH] Transform decimal precision getter into a free function. --- babel/numbers.py | 27 ++++++++++++++------------- 1 file changed, 14 insertions(+), 13 deletions(-) diff --git a/babel/numbers.py b/babel/numbers.py index 49ec7a983..2d936b935 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -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): @@ -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.""" @@ -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: