I am just learning python and have my program for Sales Tax running okay but was trying to get my input variables and floating decimal points correct. What would be the correct way to get the decimal place to show the money value with just 2 decimal places.

I have looked through the links and found these helpful links but was still trying to grasp the decimal and cents units better here.

Seems I might have found my answer with this link but will leave the question open for others to learn.

How can I format 2 decimals in Python

Decimals to 2 places for money Python

Two Decimal Places For Money Field

Money and 2 Decimal places.

It seems if I enter 5 as my price, I get 5.300000000000001

I am more familiar with SQL than programming and Python so I'm still learning.

Thanks for your time.

# 01/22/14
# Python Program Sales Tax
#
#Design a program that will ask the user to enter the amount of a purchase. 
#The program should then compute the state and county sales tax.  Assume the 
#state sales tax is 4 percent and the countysalestax is 2 percent.  The program 
#should display the amount of the purchase, the state sales tax, the county 
#sales tax, the total sales tax, and the total of the sale (which is the sum of 
#the amount of purchase plus the total sales tax)
#Use the value 0.02, and 0.04

# Display "Enter item_price Amount "
# Input item_price
# Display "state_sales_tax is 4% "
# Set state_sales_tax = 0.04
# Display "county_sales_tax is 2% "
# Set county_sales_tax = 0.02
# print("Your total cost is $",total_price,".",sep="") 


county_tax_rate = 0.02 
state_tax_rate = 0.04 
tax_rate = county_tax_rate + state_tax_rate

item_price = float(input("Please enter the price of your item.\n")) 
total_tax_rate = county_tax_rate + state_tax_rate
total_price = item_price * (1 + tax_rate) 
print("Your Total Sales Cost is $",total_price,".",sep="")
print("Your Purchase Amount was $",item_price,".",sep="") 
print("Your County Tax Rate was $", county_tax_rate,".",sep="") 
print("Your State Tax Rate was $", state_tax_rate,".",sep="") 
print("Your Total Tax Rate was $", total_tax_rate,".",sep="") 
print("Your Total Tax Rate was $", total_tax_rate,".",sep="") 
有帮助吗?

解决方案

When working with dollar amounts, I would recommend converting everything into cents. So multiply everything (except the tax rates, of course), by 100, then do any arithmetic on it, and finally convert it back to a float. Also, tax_rate and total_tax_rate are equivalent, so just use one. I would change the above to:

county_tax_rate = 0.02 
state_tax_rate = 0.04 
tax_rate = county_tax_rate + state_tax_rate

item_price = float(input("Please enter the price of your item: "))
item_price = int(100 * item_price) # Item price in cents
total_price = item_price * (1 + tax_rate) # Total price in cents

print("Your Total Sales Cost is ${:0.2f}".format(total_price / 100.0))
print("Your Purchase Amount was ${:0.2f}".format(item_price / 100.0))
print("Your County Tax Rate was {}%".format(int(county_tax_rate * 100)))
print("Your State Tax Rate was {}%".format(int(state_tax_rate * 100)))
print("Your Total Tax Rate was {}%".format(int(tax_rate * 100)))

The {:0.2f} format string takes a float and displays it out to 2 decimal places.

Note: I'm not sure why you were displaying tax rates as dollar amounts, so I changed those to percentages instead.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top