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

parse_decimal raises error for decimals with thousand separator and trailing zeroes #928

Closed
quantumdecoherence opened this issue Nov 2, 2022 · 1 comment · Fixed by #1042

Comments

@quantumdecoherence
Copy link

Bug report

parse_decimal raises a surprising NumberFormatError when parsing numbers with thousand separator and non-zero decimals but trailing zeros, when strict mode is set:

In [1]: parse_decimal('3,400.60',locale='en',strict=True)`
---------------------------------------------------------------------------`
NumberFormatError                         Traceback (most recent call last)
<ipython-input-6-ccc56249e0c2> in <module>
----> 1 parse_decimal('3,400.60', locale='en', strict=True)

~/iit_repos/hrreport/.tox/test/lib/python3.10/site-packages/babel/numbers.py in parse_decimal(string, locale, strict)
    765                     ), suggestions=[proper])
    766                 else:
--> 767                     raise NumberFormatError((
    768                         "%r is not a properly formatted decimal number. Did you mean %r? Or maybe %r?" %
    769                         (string, proper, proper_alt)

NumberFormatError: '3,400.60' is not a properly formatted decimal number. Did you mean '3,400.6'? Or maybe '3.4006'?`

Working variants:

In [2]: parse_decimal('3400.60', locale='en', strict=True)
Out[2]: Decimal('3400.60')

In [3]: parse_decimal('3,400.6', locale='en', strict=True)
Out[3]: Decimal('3400.6')

In [4]: parse_decimal('3,400.00', locale='en', strict=True)
Out[4]: Decimal('3400.00')

It is surprising and inconsistent that the presence or absence of the thousand separator should effect the result so.

Your environment

  • CPython versions tested on: 3.11.0, 3.10.6
  • Babel versions tested on: 2.10.3, 2.11.0
  • locales tested: 'en', 'de'
  • Operating system and architecture: 5.15.0-52-generic #58-Ubuntu x86_64 GNU/Linux (up-to-date Ubuntu 22.04 LTS)

Additional Information

Thanks for the great library!

@akx akx added this to the Babel 2.12 milestone Nov 2, 2022
@cshel16
Copy link

cshel16 commented Nov 10, 2022

HI, I'd like to help with this!

@akx akx modified the milestones: Babel 2.12, Babel 2.13 Feb 28, 2023
@akx akx closed this as completed in #1042 Nov 28, 2023
Changaco added a commit to liberapay/liberapay.com that referenced this issue Apr 24, 2024
Changaco added a commit to liberapay/liberapay.com that referenced this issue Apr 24, 2024
A bug in my implementation was reported upstream a year and a half ago (<python-babel/babel#928>), but I found out about that only yesterday while reviewing Babel's changelog.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants