Skip to content

Instantly share code, notes, and snippets.

@graup
Created January 10, 2017 16:56
Show Gist options
  • Save graup/7343ef1c643d66ea9ec003f71476ed85 to your computer and use it in GitHub Desktop.
Save graup/7343ef1c643d66ea9ec003f71476ed85 to your computer and use it in GitHub Desktop.
The reason why you should be careful with currency amount calculation
>>> from decimal import *
>>> VAT_RATE = Decimal('0.19')
>>> amount = Decimal('12.12')
>>> ((amount / (VAT_RATE + 1)).quantize(Decimal('1.00')) * (1+VAT_RATE)).quantize(Decimal('1.00'))
Decimal('12.11')
>>> ((amount / (VAT_RATE + 1)).quantize(Decimal('1.0000')) * (1+VAT_RATE)).quantize(Decimal('1.00'))
Decimal('12.12')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment