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

Support for decimal.Decimal and str with format_compact_decimal #929

Closed
DenverCoder1 opened this issue Nov 13, 2022 · 0 comments · Fixed by #930
Closed

Support for decimal.Decimal and str with format_compact_decimal #929

DenverCoder1 opened this issue Nov 13, 2022 · 0 comments · Fixed by #930

Comments

@DenverCoder1
Copy link
Contributor

Overview Description

Currently only float (or compatible types including int) work with format_compact_decimal.

Using decimal.Decimal in format_compact_decimal results in unsupported operand type(s) for /: 'decimal.Decimal' and 'float'

Using str results in bad operand type for abs(): 'str'

Steps to Reproduce

>>> from babel.numbers import *
>>> import decimal
>>> format_compact_decimal(decimal.Decimal("1000"))
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "babel/numbers.py", line 450, in format_compact_decimal
    number, format = _get_compact_format(number, format_type, locale, fraction_digits)
  File "babel/numbers.py", line 472, in _get_compact_format
    number = number / (magnitude / (10 ** (pattern.count("0") - 1)))
TypeError: unsupported operand type(s) for /: 'decimal.Decimal' and 'float'
>>> format_compact_decimal("1000")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "babel/numbers.py", line 450, in format_compact_decimal
    number, format = _get_compact_format(number, format_type, locale, fraction_digits)
  File "babel/numbers.py", line 463, in _get_compact_format
    if abs(number) >= magnitude:
TypeError: bad operand type for abs(): 'str'

Expected Results

Just like format_decimal, it should probably return the correctly formatted number.

Additional Information

Possibly we can cast it to a float or even int in the function to get the compact format

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant