2017-05-25 45 views
1

所以我想打印一個浮點數作爲整數。我有一個名爲「百分比」的浮點數,應該是百分比= 36.1,我想打印它爲一個整數,逗號缺失後的數字。在Python中將浮點數打印爲整數

我使用下面的代碼,這是更象用C邏輯:

percentage=36.1 
print "The moisture percentage is at %d %.", percentage 

但是,這給出了一個錯誤。我將如何改革它,以便它在Python中起作用?我想打印的是: 「水分百分比是36%」。

+1

'INT(百分比)' – Barmar

回答

5
percentage=36.1 
print "The moisture percentage is at %i%% " %percentage 
2

string format specification具有百分比已經(其中1.0100%):

percentage=36.1 
print("The moisture percentage is at {:.0%}".format(percentage/100)) 

其中%爲百分比格式指定符和.0防止任何位數要打印的逗號之後。 %-sig將自動添加。

通常這個百分比只是一個分數(沒有100的因子)。與percentage = 0.361在第一個地方將不需要除以100

2
percentage=36.1 
print "The moisture percentage is at %d %s"%(percentage,'%') 
0

您可以在python docs中看到不同的格式選項。

print "The moisture percentage is at {0:.0f} %.".format(percentage) 
0

python3.x

percentage=36.1 
print("The moisture percentage is at "+str(int(percentage))+"%")