2013-12-09 23 views
2

我在畫布上繪製圖形並插入畫布文本對象以標記x和y軸間隔。檢查float是否是Python中的整數

這是我的代碼。

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的整數,那麼我想向它們顯示沒有.00s(我有%.1f將其限制爲小數點後一位)。有沒有辦法做到這一點?

EDIT 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.

謝謝!

+1

相關,我懷疑:最Python的方式來打印\ *最多\ *一些小數位數(http://stackoverflow.com/q/14997799) –

回答

11

is_integer功能蟒蛇浮子式:

>>> float(1.0).is_integer() 
True 
>>> float(1.001).is_integer() 
False 
>>>