-
Notifications
You must be signed in to change notification settings - Fork 459
Allow precision specification. #1010
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Allow precision specification. #1010
Conversation
When bypassing decimal quantization, we should be able to specify precision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to overlap with the previously (#494, #577) soft-deprecated frac_prec
override... Would it be enough to set pattern.frac_prec = (precision, precision)
for the pattern
instead of adding the new parameter here in the bowels of Pattern
?
Further, I think an exception should be raised when trying to use the mutually exclusive arguments?
@akx,
|
when user specifies precision without setting decimal_quantization to False, we want to raise an exception
I was looking in a similar issue. In my case, I also wanted to use the implicit precision coming with a I came up with the following function to compute the value of the def _get_frac_prec(
pattern: babel.numbers.NumberPattern,
value: float | decimal.Decimal | str,
currency: str | None = None,
currency_digits: bool = True,
decimal_quantization: bool = True,
decimal_normalization: bool = True,
precision: int | None = None,
) -> tuple[int, int]:
if precision is not None:
min_prec = max_prec = precision
elif currency and currency_digits:
min_prec = max_prec = babel.numbers.get_currency_precision(currency)
else:
min_prec, max_prec = pattern.frac_prec
if decimal_quantization and (
pattern.exp_prec is None
or min_prec != 0
or max_prec != 0
):
return min_prec, max_prec
# Duplicates the beginning of NumberPattern.apply
if not isinstance(value, decimal.Decimal):
value = decimal.Decimal(str(value))
value = value.scaleb(pattern.scale)
num_prec = get_decimal_precision(value, normalized=decimal_normalization)
return max(num_prec, min_prec), max(num_prec, max_prec) Which requires to add a new parameter to def get_decimal_precision(number: decimal.Decimal, normalized: bool = True) -> int:
assert isinstance(number, decimal.Decimal)
if normalized:
number = number.normalize()
exponent = number.as_tuple().exponent
# Note: DecimalTuple.exponent can be 'n' (qNaN), 'N' (sNaN), or 'F' (Infinity)
if not isinstance(exponent, int) or exponent >= 0:
return 0
return abs(exponent) When |
@olunusib Did you close this on purpose or just resignated? I found this PR useful and it was some functionality that would be really useful to have. |
@FelixSchwarz It was inadvertently closed. I'll get it back up very soon. |
Hey @olunusib, I'm just wondering about getting this back up? Seems like some generally useful functionality and it would help me get rid of the warnings I am getting because I'm currently using |
When bypassing decimal quantization, we should be able to specify precision. Fixes #893.