Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore force_frac to NumberPattern.apply() (as deprecated) #577

Merged
merged 1 commit into from
May 18, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 35 additions & 4 deletions babel/numbers.py
Original file line number Diff line number Diff line change
Expand Up @@ -767,12 +767,38 @@ def scientific_notation_elements(self, value, locale):
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))
Expand All @@ -789,9 +815,14 @@ def apply(

# 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
Expand Down