This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: coercing decimal to int doesn't work between -1 and 1
Type: Stage:
Components: None Versions:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: rhettinger Nosy List: annarave, rhettinger
Priority: high Keywords:

Created on 2004-11-23 10:14 by annarave, last changed 2022-04-11 14:56 by admin. This issue is now closed.

Messages (2)
msg23270 - (view) Author: Anna Ravenscroft (annarave) Date: 2004-11-23 10:14
When trying to coerce a Decimal instance to int, it
works just fine if you have a value over 1. It does not
work for decimal values between1 and -1. For example:

>>> d
Decimal("1.1")
>>> s
Decimal("0.5")
>>> int(d)
1
>>> int(s)

Traceback (most recent call last):
  File "<pyshell#13>", line 1, in -toplevel-
    int(s)
  File "C:\Python24c1\lib\decimal.py", line 1420, in
__int__
    return int(s)
ValueError: invalid literal for int(): 
>>> n
Decimal("-0.5")
>>> int(n)

Traceback (most recent call last):
  File "<pyshell#16>", line 1, in -toplevel-
    int(n)
  File "C:\Python24c1\lib\decimal.py", line 1420, in
__int__
    return int(s)
ValueError: invalid literal for int(): -
>>> float(n)
-0.5
>>> i
Decimal("-1.5")
>>> int(i)
-1

Decimal coercion should work the same regardless of the
value. Outside this range (-1:1), it simply truncates
to the nearest integer. Inside this range, it returns a
ValueError. It should truncate to 0, to follow the way
the rest of Decimal coercion works.
msg23271 - (view) Author: Raymond Hettinger (rhettinger) * (Python committer) Date: 2004-11-24 07:31
Logged In: YES 
user_id=80475

Thanks for the report.
Fixed and added test cases.

Lib/decimal.py 1.31
Lib/test/test_decimal.py 1.15
History
Date User Action Args
2022-04-11 14:56:08adminsetgithub: 41213
2004-11-23 10:14:28annaravecreate