0

this snippet works properly expect for the fact that the precision is not set. Outputs have many decimals after the comma, I want it to be set to 2. Tried many ways to set it but none of them have worked. Any ideas how I can reach this?

Many thanks.

def getExponential():

x2 = entry2.get()
label2 = Label(window, text=float(x2)*float(x2))
canvas1.create_window(350, 100, window=label2)

button2 = Button(text='Get the Exponential', command=getExponential)
canvas1.create_window(240, 100, window=button2)
PurpleHaze
  • 21
  • 6

1 Answers1

0

You need to format your value, considerfollowing example:

sqrt2 = 2**0.5
print('{:.3f}'.format(sqrt2))

output

1.414

In your case just replace

label2 = Label(window, text=float(x2)*float(x2))

using

label2 = Label(window, text='{:.2f}'.format(float(x2)*float(x2)))

If you want to know more about .format usage read Format String Syntax or Format Specification Mini-Language in docs.

Daweo
  • 31,313
  • 3
  • 12
  • 25