Issue 17072: Decimal, quantize, round and negative value (original ) (raw )Created on 2013-01-29 16:14 by Hakim.Taklanti , last changed 2022-04-11 14:57 by admin . 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) *
Date: 2013-01-29 16:15
Indeed, that looks wrong. I'll take a look.
msg180913 - (view)
Author: Mark Dickinson (mark.dickinson) *
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) *
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
2022-04-11 14:57:41
admin
set
github: 61274
2013-02-10 20:40:05
Hakim.Taklanti
set
messages: +
2013-02-10 18:26:47
mark.dickinson
set
messages: +
2013-01-29 16:36:50
Hakim.Taklanti
set
messages: +
2013-01-29 16🔞38
mark.dickinson
set
status: open -> closedresolution: not a bugmessages: +
2013-01-29 16:15:34
mark.dickinson
set
assignee: mark.dickinson messages: + nosy: + mark.dickinson
2013-01-29 16:14:16
Hakim.Taklanti
create