[英]2 decimal places instead of one, but code is not requiring a float
我試圖了解如何在此代碼中獲得2個小數位。 科迪奧將其視為一本。 它不需要浮動命令。 如果我要輸入一個浮點數,那么我將實際在此字符串中添加$%。2f命令。 我對編碼非常陌生,所以我只想確保自己在創建正確的語法。 已經為此工作了幾個小時。
if rentalCode == "B":
budgetCharge = 40.00
baseCharge = rentalPeriod * budgetCharge
elif rentalCode == "D":
budgetCharge = 60.00
baseCharge = rentalPeriod * budgetCharge
elif rentalCode == "W":
budgetCharge = 190.00
baseCharge = rentalPeriod * budgetCharge
print(baseCharge)
用兩位小數定義budgetCharge
將budgetCharge
。 您可以使用格式字符串指定小數位數:
# old style
print('%.2f' % baseCharge)
# new style
print('{:.2f}'.format(baseCharge))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.