Skip to content

Instantly share code, notes, and snippets.

@aesmail
Created April 27, 2016 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aesmail/56aaf159e44b284dcfbcbf84d0df4f04 to your computer and use it in GitHub Desktop.
Save aesmail/56aaf159e44b284dcfbcbf84d0df4f04 to your computer and use it in GitHub Desktop.
Python: Difference when passing a number or a string to decimal.Decimal
# Scenario ONE
price = Decimal(33.000) # <-- passed in a number
factor = Decimal(0.3020)
price = price / factor
price = price * factor
price.quantize(Decimal("0.01"), rounding=ROUND_UP) # --> 33
# Scenario TWO
price = Decimal('33.000') # <-- passed in a string
factor = Decimal('0.3020')
price = price / factor
price = price * factor
price.quantize(Decimal("0.01"), rounding=ROUND_UP) # --> 33.01
@sylwekb
Copy link

sylwekb commented Apr 27, 2016

actually the few first bullet points there answer your question https://docs.python.org/2.7/library/decimal.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment