[英]How to round to 2 decimals in python tkinter
我正在制作一個計算百分比的小程序,但我找不到將結果四舍五入到兩位小數的方法。
我相信我需要在 function 中使用類似"{:.2f}"
的東西,但我不知道具體在哪里
這是我的代碼的一部分,我認為我必須在其中放置命令:
# Defining a function that takes any arguments and calulcates the proportion of the population and the vaccinated
def calculate(*args):
try:
popint = float(pop.get())
vaccint = float(vacc.get())
rate.set((vaccint / popint) * 100)
except ValueError:
pass # Ignore for now
# Defining a function that increases by 1 the number of vaccinated
def addvacc(*args):
try:
vaccadd = int(vacc.get())
vacc.set(vaccadd + 1)
calculate()
except ValueError:
pass # Ignore for now
# Creating a window with title
root = Tk()
一切都已創建(按鈕、標簽等),並且可以正常工作。
我附上了程序的圖形用戶界面。 謝謝。
一旦你得到百分比,當你這樣做時:
round(<your percentage value>,2) #syntax: round(Value, digits to be rounded off to)
我只是取了一個任意的浮點值並執行了:
round(75.678780,2)
Output: 75.68
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.