Rounding of decimal places float python

Asked

Viewed 182 times

0

Hello, I am starting now to learn programming and I made a calculator that makes the calculation of the formula Bhaskara and it has interface, I used the Tkinter.

My doubt is the following I wanted the number appearing in a mathematical formula to have 4 or 5 decimal places. I read on some forums about using the round or decimal, and I was able to but not for my code, because the value I want to arrendondar is in variable Rd, in Rd has a delta root formula, for example, and I couldn’t get a form that rounded it. And this result 'Rd' this in will be shown in a label, and I believe that this is the most difficult point if only to use the print would work but is in a label. I will leave the code and if you have any hint to shorten the code I thank you, as I do so at label date a number of decimal places I want?

python3


def cof_a():
    a=float(eda.get())
    b=float(edb.get())
    c=float(edc.get())
    cof_z=(b**2-4*a*c)

    lb_delta['text']=cof_z


def raiz_d():
    a=float(eda.get())
    b=float(edb.get())
    c=float(edc.get())
    rd=((b**2-4*a*c)**0.5)

    lb_rdelta['text']=rd


def x1():#funçao para raiz de x1
    a=float(eda.get())
    b=float(edb.get())
    c=float(edc.get())
    rx1=(-b+((b**2-4*a*c)**0.5))/(2*a)

    lb_x1['text']=rx1


def x2():#funçao para raiz de x2
    a=float(eda.get())
    b=float(edb.get())
    c=float(edc.get())
    rx2=(-b-((b**2-4*a*c)**0.5))/(2*a)

    lb_x2['text']=rx2


#######################LABELS DE A,B,C####################################
lba=Label(janela , text='Digite o valor do coeficiente de "a" em ax² ', bg='#006400', fg='white', font='verdana')
lba.place(x=30, y=40)
eda=Entry(janela, bg='white', fg='black', font='verdana')
eda.place(x=420, y=40)

lbb=Label(janela , text='Digite o valor do coeficiente de "b" em bx   ', bg='#006400', fg='white', font='verdana')
lbb.place(x=30, y=80)
edb=Entry(janela, bg='white', fg='black', font='verdana')
edb.place(x=420, y=80)

lbc=Label(janela , text='Digite o valor do coeficiente de "c" em c     ', bg='#006400', fg='white', font='verdana')
lbc.place(x=30, y=120)
edc=Entry(janela, bg='white', fg='black', font='verdana')
edc.place(x=420, y=120)
##################


###############LABEL VALOR DE DELTA##################################
lb_delta=Label(janela, bg='#6B8E23', fg='white', font='verdana')
lb_delta.place(x=380, y=240)

lbcof=Label(janela , text='O Delta ou Binômio discriminante é =', bg='#006400', fg='white', font='verdana')
lbcof.place(x=30, y=240)

#########################LABEL RAIZ DE DELTA##############################
lb_rdelta=Label(janela, bg='#6B8E23', fg='white', font='verdana')
lb_rdelta.place(x=380, y=280)

lb_raizdelta=Label(janela , text='O valor da raiz quadrada do delta é =', bg='#006400', fg='white', font='verdana')
lb_raizdelta.place(x=30, y=280)

#########################LABELS X1 E X2############################
lb_x1=Label(janela , text='A raiz x1 é ', bg='#006400', fg='white', font='verdana')
lb_x1.place(x=60, y=360)

lb_x1=Label(janela, bg='#6B8E23', fg='white', font='verdana')
lb_x1.place(x=60, y=400)


lb_x2=Label(janela , text='A raiz x2 é ', bg='#006400', fg='white', font='verdana')
lb_x2.place(x=360, y=360)

lb_x2=Label(janela, bg='#6B8E23', fg='white', font='verdana')
lb_x2.place(x=360, y=400)
###############################################################################

  • Maybe using the round function?

1 answer

0

The part of your code, try:

def raiz_d():
    a=float(eda.get())
    b=float(edb.get())
    c=float(edc.get())
    rd=((b**2-4*a*c)**0.5)
    rd = round(rd,2)
    lb_rdelta['text']=rd

In a direct test (passing the values manually), the result was:

a 10
b 20
c 3
16.73

And without the round() would be 16.73320053068151

Browser other questions tagged

You are not signed in. Login or sign up in order to post.