我剛剛學習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