简体   繁体   English

Python 小费计算器四舍五入到小数点后两位

[英]Tip calculator rounding to 2 decimal places in Python

I am having a problem with rounding the final result to 2 decimal places in Python. It always shows up only 1 decimal.我在将 Python 中的最终结果四舍五入到小数点后两位时遇到问题。它始终只显示小数点后一位。 eg 33.6$ I want to have it as 33.60$例如 33.6$ 我想把它变成 33.60$

    #tip calculator
bill = float (input("what is the total bill? "))
tip = float (input ("percentage tip 10%, 20% etc. ? "))
ppl = int (input ("number of people to split the bill? "))

tip_decimal = tip/100+1
calc = (bill/ppl)*tip_decimal
calc2 = round(calc, 2)
print(f"each person should pay {calc2}$")

use formating here.在这里使用格式化。

bill = float (input("what is the total bill? "))
tip = float (input ("percentage tip 10%, 20% etc. ? "))
ppl = int (input ("number of people to split the bill? "))

tip_decimal = tip/100+1
calc = (bill/ppl)*tip_decimal # change calc2 to calc in print
# calc2 = round(calc, 2) # you also not need this line of code.
print(f"each person should pay {calc:.2f}$") # here `:.2f` add zero at end if there is no second digit

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM