我刚刚学习python,并有我的销售税计划运行没关系,但试图让我的输入变量和浮动小数点正确。以小数点后两位显示货币值的正确方法是什么?Python销售税计划与2个小数位的地方,钱
我已经查看了这些链接,发现了这些有用的链接,但仍试图在这里更好地掌握小数和美分单位。
似乎我可能已通过此链接找到了我的答案,但会将问题留给其他人学习。
How can I format 2 decimals in Python
Decimals to 2 places for money Python
Two Decimal Places For Money Field
看来,如果我进入5作为我的价格,我得到5.300000000000001
我比较熟悉SQL比编程和Python,所以我还在学习。
谢谢你的时间。
# 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="")
您链接到答案至少两次。浮点运算是不精确的;要么使用字符串格式,要么使用'Decimals'。 – roippi