Created
April 27, 2016 10:17
-
-
Save aesmail/56aaf159e44b284dcfbcbf84d0df4f04 to your computer and use it in GitHub Desktop.
Python: Difference when passing a number or a string to decimal.Decimal
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
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
actually the few first bullet points there answer your question https://docs.python.org/2.7/library/decimal.html