2017-03-06 28 views
0

我有下面的代碼顯示我的結果作爲一個double但是我想要顯示它爲int?這是我已經工作的代碼在文本視圖中顯示結果爲int

Bundle b = getIntent().getExtras(); 
double result = b.getDouble("total"); 

TextView display = (TextView)findViewById(R.id.txtResult); 
display.setText(+result+ "well done"); 

回答

0

你可以施放它如果結果爲雙原始的,這是迫使雙是一個整數,失去小數部分....

display.setText((int)result+ "well done"); 

如果結果爲雙包裝,然後調用該方法雙擊#的intValue()

display.setText(result.intValue()+ "well done"); 
+0

完美奏效,謝謝您! – gw95

+0

歡迎... –

-1

你應該設置文本象下面這樣:

display.setText((int)result + "well done"); 
0

哪一個你想要的:
1雙到整數
2雙爲int
3雙爲int
4雙到整數

1 double轉換成整數不直接可能。你必須轉換雙翻番,然後將其轉換雙爲int,並在年底將int轉換成整型

Double D= Double.valueOf(5.0); 
double d= D.doubleValue(); 
int i=(int)d; 
Integer I= Integer.valueOf(i); 

可以論文集線2和3是這樣的:

int i= D.intValue(); 

2 - 雙爲int

Double D= Double.valueOf(5.0); 
int i= D.intValue(); 

3-雙爲int:

double d; 
int i=(int)d 

ASLO您可以使用輪第2行:

int i=Math.round(d); 

4-雙到整數

double d; 
int i=(int)d 
Integer I= Integer.valueOf(i); 
再次

您可以使用輪第2行:

int i=Math.round(d);