Skip to content

Instantly share code, notes, and snippets.

@ejamesc
Last active March 9, 2016 14:55
Show Gist options
  • Save ejamesc/cedc886c5f36e2d075c5 to your computer and use it in GitHub Desktop.
Save ejamesc/cedc886c5f36e2d075c5 to your computer and use it in GitHub Desktop.
Solve rounding issues due to floating point repr in Python
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