在Python中检查浮点是否为整数

在Python中检查浮点是否为整数,python,floating-point,int,Python,Floating Point,Int,我正在画布上绘制图形,并插入画布文本对象来标记x轴和y轴间隔 这是我的密码 def update(): scalex1=graph.create_text(356,355-87,text='%.1f'%scalex,anchor=NW) scalex2=graph.create_text(412,355-87,text='%.1f'% (scalex*2),anchor=NW) scalex3=graph.create_text(471,355-87,text='%.1f

我正在画布上绘制图形,并插入画布文本对象来标记x轴和y轴间隔

这是我的密码

def update():
    scalex1=graph.create_text(356,355-87,text='%.1f'%scalex,anchor=NW)
    scalex2=graph.create_text(412,355-87,text='%.1f'% (scalex*2),anchor=NW)
    scalex3=graph.create_text(471,355-87,text='%.1f'%(scalex*3),anchor=NW)
    scalex4=graph.create_text(532,355-87,text='%.1f'%(scalex*4),anchor=NW)

    scalenx1=graph.create_text(255-23,355-87,text='%.1f'%(scalex*-1),anchor=NW)
    scalenx2=graph.create_text(195-25,355-87,text='%.1f'% (scalex*-2),anchor=NW)
    scalenx3=graph.create_text(135-25,355-87,text='%.1f'%(scalex*-3),anchor=NW)
    scalenx4=graph.create_text(66-18,355-87,text='%.1f'%(scalex*-4),anchor=NW)


    scaley1=graph.create_text(326,234,text='%.1f'%scaley,anchor=NW)
    scaley2=graph.create_text(326,174,text='%.1f'% (scaley*2),anchor=NW)
    scaley3=graph.create_text(326,114,text='%.1f'%(scaley*3),anchor=NW)
    scaley4=graph.create_text(326,54,text='%.1f'%(scaley*4),anchor=NW)

    scaleny1=graph.create_text(326,354,text='%.1f'%(scaley*-1),anchor=NW)
    scaleny2=graph.create_text(326,414,text='%.1f'%(scaley*-2),anchor=NW)
    scaleny3=graph.create_text(326,474,text='%.1f'%(scaley*-3),anchor=NW)
    scaleny4=graph.create_text(326,534,text='%.1f'%(scaley*-4),anchor=NW)
这将根据输入的比例绘制所有间隔

x2=float(x1.get())
y2=float(y1.get())

scalex=5.0*(x2/25.0)
scaley=5.0*(y2/25.0)
这将确定x轴和y轴上每个记号的比例,并将其乘以5以获得每五个记号的值。有25个刻度,因此我将输入除以/25

我想显示这些值,但它们都是浮点数。如果它们是真正的整数,我想显示它们不带.00s(我有%.1f将其限制在小数点后1位),如果它们实际上是浮点数,而不是带.00s的整数,则显示它们最多2位小数。有办法做到这一点吗

编辑1: 例如,比例为25,因此每个刻度为1。第五个刻度将为-10.0、-5.0、5.0、10.0、15.0等。 我希望它显示10,5,15等,但如果是.5,1.0,1.5,2.0 我想保留所有整数的小数,但真正的整数除外,比如1.0和2.0,它们将变成1和2

谢谢

python float类型中有一个函数:

>>> float(1.0).is_integer()
True
>>> float(1.001).is_integer()
False
>>> 
相关的,我怀疑: