diff options
author | Aarni Koskela <akx@iki.fi> | 2018-05-18 12:17:26 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2018-05-18 12:17:26 +0200 |
commit | 8060b2cdc84ad6d3f24cfb1aa08276c964be2892 (patch) | |
tree | 43811fff039c506d2c4f234f75fb5e1239b32019 | |
parent | a27176d3c212f7fd82274e2392bb7fd4d919d784 (diff) | |
parent | 1377559c830d6c3166f1e1bdced9953e0f4045fa (diff) | |
download | babel-8060b2cdc84ad6d3f24cfb1aa08276c964be2892.tar.gz |
Merge pull request #577 from akx/pattern-prec
Restore force_frac to NumberPattern.apply() (as deprecated)
-rw-r--r-- | babel/numbers.py | 39 |
1 files changed, 35 insertions, 4 deletions
diff --git a/babel/numbers.py b/babel/numbers.py index cbd1474..a3a93a6 100644 --- a/babel/numbers.py +++ b/babel/numbers.py @@ -767,12 +767,38 @@ class NumberPattern(object): return value, exp, exp_sign def apply( - self, value, locale, currency=None, currency_digits=True, - decimal_quantization=True): + self, + value, + locale, + currency=None, + currency_digits=True, + decimal_quantization=True, + force_frac=None, + ): """Renders into a string a number following the defined pattern. Forced decimal quantization is active by default so we'll produce a number string that is strictly following CLDR pattern definitions. + + :param value: The value to format. If this is not a Decimal object, + it will be cast to one. + :type value: decimal.Decimal|float|int + :param locale: The locale to use for formatting. + :type locale: str|babel.core.Locale + :param currency: Which currency, if any, to format as. + :type currency: str|None + :param currency_digits: Whether or not to use the currency's precision. + If false, the pattern's precision is used. + :type currency_digits: bool + :param decimal_quantization: Whether decimal numbers should be forcibly + quantized to produce a formatted output + strictly matching the CLDR definition for + the locale. + :type decimal_quantization: bool + :param force_frac: DEPRECATED - a forced override for `self.frac_prec` + for a single formatting invocation. + :return: Formatted decimal string. + :rtype: str """ if not isinstance(value, decimal.Decimal): value = decimal.Decimal(str(value)) @@ -789,9 +815,14 @@ class NumberPattern(object): # Adjust the precision of the fractionnal part and force it to the # currency's if neccessary. - frac_prec = self.frac_prec - if currency and currency_digits: + if force_frac: + # TODO (3.x?): Remove this parameter + warnings.warn('The force_frac parameter to NumberPattern.apply() is deprecated.', DeprecationWarning) + frac_prec = force_frac + elif currency and currency_digits: frac_prec = (get_currency_precision(currency), ) * 2 + else: + frac_prec = self.frac_prec # Bump decimal precision to the natural precision of the number if it # exceeds the one we're about to use. This adaptative precision is only |