classification
Title: Decimal, quantize, round and negative value
Type: Stage:
Components: Library (Lib) Versions: Python 2.7
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: mark.dickinson Nosy List: Hakim.Taklanti, mark.dickinson
Priority: normal Keywords:

Created on 2013-01-29 16:14 by Hakim.Taklanti, last changed 2013-02-10 20:40 by Hakim.Taklanti. This issue is now closed.

Messages (6)
msg180911 - (view) Author: Hakim Taklanti (Hakim.Taklanti) Date: 2013-01-29 16:14
>>> from decimal import Decimal
>>> from decimal import ROUND_UP, ROUND_DOWN
>>> a = Decimal("-3.86")
>>> b = Decimal("5.73")
>>> a_up = a.quantize(Decimal(".1"), ROUND_UP)
>>> a.quantize(Decimal(".1"), ROUND_UP) # -3.8 expected
Decimal('-3.9') 
>>> a.quantize(Decimal(".1"), ROUND_DOWN) # -3.9 expected
Decimal('-3.8') 
>>> b.quantize(Decimal(".1"), ROUND_UP) # Ok
Decimal('5.8')
>>> b.quantize(Decimal(".1"), ROUND_DOWN) # Ok
Decimal('5.7')
msg180912 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-01-29 16:15
Indeed, that looks wrong.  I'll take a look.
msg180913 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-01-29 16:18
Sorry, I take that back.  The behaviour is correct:  ROUND_UP rounds away from zero;  ROUND_DOWN towards zero.  For rounding towards +/- infinity, you want ROUND_CEILING and ROUND_FLOOR:


Python 2.7.3 |EPD 7.3-1 (32-bit)| (default, Apr 12 2012, 11:28:34) 
[GCC 4.0.1 (Apple Inc. build 5493)] on darwin
Type "credits", "demo" or "enthought" for more information.
>>> from decimal import *
>>> a = Decimal("-3.86")
>>> a.quantize(Decimal(".1"), ROUND_CEILING)
Decimal('-3.8')
>>> a.quantize(Decimal(".1"), ROUND_FLOOR)
Decimal('-3.9')


Closing as invalid.
msg180914 - (view) Author: Hakim Taklanti (Hakim.Taklanti) Date: 2013-01-29 16:36
Indeed, perhaps to enhance the documentation. Thanks.
msg181829 - (view) Author: Mark Dickinson (mark.dickinson) * (Python committer) Date: 2013-02-10 18:26
It's not obvious to me how the documentation could be made clearer:  all the rounding modes are described at

http://docs.python.org/2/library/decimal.html#decimal.Context

for the Python 2 docs, and in a separate section entitled 'Rounding modes' for the Python 3 docs:

http://docs.python.org/3/library/decimal.html#rounding-modes

Did you have a particular documentation enhancement in mind?
msg181854 - (view) Author: Hakim Taklanti (Hakim.Taklanti) Date: 2013-02-10 20:40
You is right. I had just see the beginning of documentation ("Rounding options include..."). Sorry for the bother and thanks for the answer.
History
Date User Action Args
2013-02-10 20:40:05Hakim.Taklantisetmessages: + msg181854
2013-02-10 18:26:47mark.dickinsonsetmessages: + msg181829
2013-01-29 16:36:50Hakim.Taklantisetmessages: + msg180914
2013-01-29 16:18:38mark.dickinsonsetstatus: open -> closed
resolution: not a bug
messages: + msg180913
2013-01-29 16:15:34mark.dickinsonsetassignee: mark.dickinson

messages: + msg180912
nosy: + mark.dickinson
2013-01-29 16:14:16Hakim.Taklanticreate