[ python-Bugs-1071588 ] coercing decimal to int doesn't work between -1 and 1

SourceForge.net noreply at sourceforge.net
Wed Nov 24 08:31:14 CET 2004


Bugs item #1071588, was opened at 2004-11-23 05:14
Message generated for change (Settings changed) made by rhettinger
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1071588&group_id=5470

Category: None
Group: None
>Status: Closed
>Resolution: Fixed
Priority: 7
Submitted By: Anna Ravenscroft (annarave)
Assigned to: Raymond Hettinger (rhettinger)
Summary: coercing decimal to int doesn't work between -1 and 1

Initial Comment:
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.

----------------------------------------------------------------------

>Comment By: Raymond Hettinger (rhettinger)
Date: 2004-11-24 02:31

Message:
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

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=1071588&group_id=5470


More information about the Python-bugs-list mailing list