【问题标题】:Python Round decimals using tkinter [duplicate]使用tkinter的Python四舍五入小数[重复]
【发布时间】:2021-07-21 07:49:21
【问题描述】:

这个 sn-p 可以正常工作,因为没有设置精度。输出在逗号后有许多小数,我希望将其设置为 2。尝试了很多方法来设置它,但都没有奏效。有什么想法可以实现吗?

非常感谢。

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)

【问题讨论】:

    标签: python tkinter decimal precision


    【解决方案1】:

    您需要format您的价值,请考虑以下示例:

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

    输出

    1.414
    

    在你的情况下,只需替换

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

    使用

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

    如果您想了解更多关于 .format 用法的信息,请阅读文档中的 Format String SyntaxFormat Specification Mini-Language

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多