Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decimal, quantize, round and negative value #61274

Closed
HakimTaklanti mannequin opened this issue Jan 29, 2013 · 6 comments
Closed

Decimal, quantize, round and negative value #61274

HakimTaklanti mannequin opened this issue Jan 29, 2013 · 6 comments
Assignees
Labels
stdlib Python modules in the Lib dir

Comments

@HakimTaklanti
Copy link
Mannequin

HakimTaklanti mannequin commented Jan 29, 2013

BPO 17072
Nosy @mdickinson

Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

Show more details

GitHub fields:

assignee = 'https://github.com/mdickinson'
closed_at = <Date 2013-01-29.16:18:38.461>
created_at = <Date 2013-01-29.16:14:16.913>
labels = ['invalid', 'library']
title = 'Decimal, quantize, round and negative value'
updated_at = <Date 2013-02-10.20:40:05.806>
user = 'https://bugs.python.org/HakimTaklanti'

bugs.python.org fields:

activity = <Date 2013-02-10.20:40:05.806>
actor = 'Hakim.Taklanti'
assignee = 'mark.dickinson'
closed = True
closed_date = <Date 2013-01-29.16:18:38.461>
closer = 'mark.dickinson'
components = ['Library (Lib)']
creation = <Date 2013-01-29.16:14:16.913>
creator = 'Hakim.Taklanti'
dependencies = []
files = []
hgrepos = []
issue_num = 17072
keywords = []
message_count = 6.0
messages = ['180911', '180912', '180913', '180914', '181829', '181854']
nosy_count = 2.0
nosy_names = ['mark.dickinson', 'Hakim.Taklanti']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = None
url = 'https://bugs.python.org/issue17072'
versions = ['Python 2.7']

@HakimTaklanti
Copy link
Mannequin Author

HakimTaklanti mannequin commented Jan 29, 2013

>>> 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')

@HakimTaklanti HakimTaklanti mannequin added the stdlib Python modules in the Lib dir label Jan 29, 2013
@mdickinson
Copy link
Member

Indeed, that looks wrong. I'll take a look.

@mdickinson mdickinson self-assigned this Jan 29, 2013
@mdickinson
Copy link
Member

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.

@HakimTaklanti
Copy link
Mannequin Author

HakimTaklanti mannequin commented Jan 29, 2013

Indeed, perhaps to enhance the documentation. Thanks.

@mdickinson
Copy link
Member

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?

@HakimTaklanti
Copy link
Mannequin Author

HakimTaklanti mannequin commented Feb 10, 2013

You is right. I had just see the beginning of documentation ("Rounding options include..."). Sorry for the bother and thanks for the answer.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
stdlib Python modules in the Lib dir
Projects
None yet
Development

No branches or pull requests

1 participant