-
-
Notifications
You must be signed in to change notification settings - Fork 31.4k
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
Make Decimal constructor accept all unicode decimal digits in input. #50844
Comments
Ezio Melotti asked (on #python-dev) why the Decimal constructor doesn't """It is recommended that implementations also provide additional number All other builtin or standard library numeric types already accept such Python 3.2a0 (py3k:74247, Jul 29 2009, 09:28:12)
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fractions import Fraction
>>> from decimal import Decimal
>>> x = '\uff11\uff10\uff15\uff18\uff15'
>>> x
'10585'
>>> int(x)
10585
>>> float(x)
10585.0
>>> complex(x)
(10585+0j)
>>> Fraction(x)
Fraction(10585, 1)
>>> Decimal(x)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/dickinsm/python/svn/py3k/Lib/decimal.py", line 548, in
__new__
"Invalid literal for Decimal: %r" % value)
File "/Users/dickinsm/python/svn/py3k/Lib/decimal.py", line 3816, in
_raise_error
raise error(explanation)
decimal.InvalidOperation: Invalid literal for Decimal: '10585' I propose adding support for this in Python 3.2 and (possibly) 2.7. The |
Here's a patch |
+1 The standard recommends it, and the other numeric types support it, so |
Since you're calling int() on the result, can't this code: And here, you already know diag is not None, so do you need the "or '0'" And, in both calls to .lstrip('0'), what happens if you have a Otherwise, the patch looks good to me. |
+1 Also, I would like to see this backported. We've long promised that any |
Thanks for the feedback; I've added 2.6, 2.7, 3.1 to the [Eric]
Yes! Thank you. I'm not sure what (if anything) I was thinking here. The str(int(...)) hack is quite an ugly way to normalize a string; it
I think *something* like this is needed, since diag can legitimately be |
I like the str(int(...)) approach because it guarantees handling that |
Committed to py3k in r74279, release31-maint in r74280. Leaving open for |
Backported to trunk and release26-maint in r74281 and r74282. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: