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
Comments
>>> 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') |
Indeed, that looks wrong. I'll take a look. |
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. |
Indeed, perhaps to enhance the documentation. Thanks. |
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? |
You is right. I had just see the beginning of documentation ("Rounding options include..."). Sorry for the bother and thanks for the answer. |
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:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: