Message91030
Ezio Melotti asked (on #python-dev) why the Decimal constructor doesn't
accept decimal digits other than 0-9. As far as I can tell there's no
good reason for it not to. Moreover, the standard on which the decimal
module is based says[1]:
"""It is recommended that implementations also provide additional number
formatting routines (including some which are locale-dependent), and if
available should accept non-European decimal digits in strings."""
All other builtin or standard library numeric types already accept such
digits:
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
change would be for input only: no record of the original form of the
digits would be kept by the Decimal object itself, so that e.g.,
str(Decimal('10585')) would still be '10585'.
[1] See http://speleotrove.com/decimal/daconvs.html |
|
Date |
User |
Action |
Args |
2009-07-29 08:39:55 | mark.dickinson | set | recipients:
+ mark.dickinson, ezio.melotti |
2009-07-29 08:39:55 | mark.dickinson | set | messageid: <1248856795.44.0.0182588213317.issue6595@psf.upfronthosting.co.za> |
2009-07-29 08:39:53 | mark.dickinson | link | issue6595 messages |
2009-07-29 08:39:51 | mark.dickinson | create | |
|