JTiger
JTiger

Reputation: 31

Round the answer to 2 decimal points

I have a function that finds the average of 3 number that are entered by the user on a GUI. I need to be able to display the answer to 2 decimal points. I just cant find where to use the round function.

def average():
        try:
            value1 = int(month1_entry.get())
            value2 = int(month2_entry.get())
            value3 = int(month3_entry.get())
            cases_average.set((value1 + value2 + value3) / 3)
            
        except ValueError:
            messagebox.showerror("Error", "Please Enter a Whole Number")
average_label = ttk.Label(frame, textvariable = cases_average)
average_label.grid(column = 2, row = 5, sticky = (E))

Any help would be appreciated

Thanks

Upvotes: 0

Views: 36

Answers (1)

Ganesh Jadhav
Ganesh Jadhav

Reputation: 802

You can use round function

def average():
        try:
            value1 = int(month1_entry.get())
            value2 = int(month2_entry.get())
            value3 = int(month3_entry.get())
            cases_average.set(round((value1 + value2 + value3) / 3,2)) # I have changed here
            
        except ValueError:
            messagebox.showerror("Error", "Please Enter a Whole Number")

Upvotes: 1

Related Questions