如何将一个数四舍五入到n位小数?
How to round a number to n decimal places?
我要,我在输入栏中输入的内容应该自动四舍五入到小数点后n位。
import Tkinter as Tk
root = Tk.Tk()
class InterfaceApp():
def __init__(self,parent):
self.parent = parent
root.title("P")
self.initialize()
def initialize(self):
frPic = Tk.Frame(bg='', colormap='new')
frPic.grid(row=0)
a= Tk.DoubleVar()
self.entry = Tk.Entry(frPic, textvariable=a)
a.set(round(self.entry.get(), 2))
self.entry.grid(row=0)
if __name__ == '__main__':
app = InterfaceApp(root)
root.mainloop()
我猜你想要的不是对浮点值本身进行四舍五入,而是想显示一个精度为 n 位小数的浮点值。试试这个:
>>> n = 2
>>> '{:.{}f}'.format( 3.1415926535, n )
'3.14'
>>> n = 3
>>> '{:.{}f}'.format( 3.1415926535, n )
'3.142'
注意:在您的代码中,您尝试舍入 self.entry
.i。 e.您尝试舍入 Tk.Entry
类型的实例。您应该使用 self.entry.get()
为您提供字符串。
如果你不熟悉我使用的这种字符串格式,请看 here。
你没有得到预期的结果,因为当你在 initialize()
中 运行 a.set(round(self.entry, 2))
时, self.entry.get()
的值总是 0
(默认值创建后的值)
您更需要将 callback 附加到按钮小部件,按下后,您正在寻找的行为将在其上执行:
import Tkinter as Tk
root = Tk.Tk()
class InterfaceApp():
def __init__(self,parent):
self.parent = parent
root.title("P")
self.initialize()
def initialize(self):
frPic = Tk.Frame(bg='', colormap='new')
frPic.grid(row=0, column=0)
self.a = Tk.DoubleVar()
self.entry = Tk.Entry(frPic, textvariable=self.a)
self.entry.insert(Tk.INSERT,0)
self.entry.grid(row=0, column=0)
# Add a button widget with a callback
self.button = Tk.Button(frPic, text='Press', command=self.round_n_decimal)
self.button.grid(row=1, column=0)
# Callback
def round_n_decimal(self):
self.a.set(round(float(self.entry.get()), 2))
if __name__ == '__main__':
app = InterfaceApp(root)
root.mainloop()
我要,我在输入栏中输入的内容应该自动四舍五入到小数点后n位。
import Tkinter as Tk
root = Tk.Tk()
class InterfaceApp():
def __init__(self,parent):
self.parent = parent
root.title("P")
self.initialize()
def initialize(self):
frPic = Tk.Frame(bg='', colormap='new')
frPic.grid(row=0)
a= Tk.DoubleVar()
self.entry = Tk.Entry(frPic, textvariable=a)
a.set(round(self.entry.get(), 2))
self.entry.grid(row=0)
if __name__ == '__main__':
app = InterfaceApp(root)
root.mainloop()
我猜你想要的不是对浮点值本身进行四舍五入,而是想显示一个精度为 n 位小数的浮点值。试试这个:
>>> n = 2
>>> '{:.{}f}'.format( 3.1415926535, n )
'3.14'
>>> n = 3
>>> '{:.{}f}'.format( 3.1415926535, n )
'3.142'
注意:在您的代码中,您尝试舍入 self.entry
.i。 e.您尝试舍入 Tk.Entry
类型的实例。您应该使用 self.entry.get()
为您提供字符串。
如果你不熟悉我使用的这种字符串格式,请看 here。
你没有得到预期的结果,因为当你在 initialize()
中 运行 a.set(round(self.entry, 2))
时, self.entry.get()
的值总是 0
(默认值创建后的值)
您更需要将 callback 附加到按钮小部件,按下后,您正在寻找的行为将在其上执行:
import Tkinter as Tk
root = Tk.Tk()
class InterfaceApp():
def __init__(self,parent):
self.parent = parent
root.title("P")
self.initialize()
def initialize(self):
frPic = Tk.Frame(bg='', colormap='new')
frPic.grid(row=0, column=0)
self.a = Tk.DoubleVar()
self.entry = Tk.Entry(frPic, textvariable=self.a)
self.entry.insert(Tk.INSERT,0)
self.entry.grid(row=0, column=0)
# Add a button widget with a callback
self.button = Tk.Button(frPic, text='Press', command=self.round_n_decimal)
self.button.grid(row=1, column=0)
# Callback
def round_n_decimal(self):
self.a.set(round(float(self.entry.get()), 2))
if __name__ == '__main__':
app = InterfaceApp(root)
root.mainloop()