Last active
March 9, 2016 14:55
-
-
Save ejamesc/cedc886c5f36e2d075c5 to your computer and use it in GitHub Desktop.
Solve rounding issues due to floating point repr in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from decimal import Decimal, ROUND_HALF_UP | |
a = round(2.675, 2) | |
b = Decimal(repr(2.675)).quantize(Decimal('.01'), rounding=ROUND_HALF_UP) | |
print a, b # 2.67, 2.68 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment