How to represent currency in Python

1 Answer

0 votes
# ---------------------------------------------------------
# Description:
#   Demonstrates how to represent and display currency
#   values in Python using decimal.Decimal and formatting.
# ---------------------------------------------------------

from decimal import Decimal, ROUND_HALF_UP

# Price and tax rate
price = Decimal("199.99")
tax_rate = Decimal("0.17")   # 17%

# Calculate tax and total
tax_amount = (price * tax_rate).quantize(Decimal("0.01"), rounding=ROUND_HALF_UP)
total = price + tax_amount

# Output with $ and % signs
print("Price: $" + format(price, ".2f"))
print("Tax Rate: 17%")
print("Tax Amount: $" + format(tax_amount, ".2f"))
print("Total: $" + format(total, ".2f"))


'''
run:

Price: $199.99
Tax Rate: 17%
Tax Amount: $34.00
Total: $233.99

'''

 



answered 3 hours ago by avibootz
...