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

SourceForge.net noreply at sourceforge.net
Tue Nov 23 11:14:28 CET 2004


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

Category: None
Group: None
Status: Open
Resolution: None
Priority: 5
Submitted By: Anna Ravenscroft (annarave)
Assigned to: Nobody/Anonymous (nobody)
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.

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

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