Skip to content

Instantly share code, notes, and snippets.

@sailfish009
Created January 23, 2021 00:50
Show Gist options
  • Save sailfish009/c4a1987027a98e1110cc195d26e3c136 to your computer and use it in GitHub Desktop.
Save sailfish009/c4a1987027a98e1110cc195d26e3c136 to your computer and use it in GitHub Desktop.
https://stackoverflow.com/questions/10825926/python-3-x-rounding-behavior
>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'), rounding=decimal.ROUND_HALF_UP)
Decimal('4')
>>> decimal.Decimal('2.5').quantize(decimal.Decimal('1'), rounding=decimal.ROUND_HALF_EVEN)
Decimal('2')
>>> decimal.Decimal('3.5').quantize(decimal.Decimal('1'), rounding=decimal.ROUND_HALF_DOWN)
Decimal('3')
>>> decimal.Decimal('0.125').quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_UP)
Decimal('0.13')
>>> decimal.Decimal('0.135').quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_UP)
Decimal('0.14')
>>> decimal.Decimal('0.175').quantize(decimal.Decimal('0.00'), rounding=decimal.ROUND_HALF_UP)
Decimal('0.18')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment