2015-02-11 60 views
0

我寫了學習java的android計算器,並且在應用程序中出現錯誤。如果我輸入5/3 - 答案是2,但我需要浮動結果(1,6)。我需要改變什麼?Java Android:導致計算器顯示整數(需要float/double)

ArrayList<Float> result = new ArrayList<Float>(); 

    float number1; 
    float number2; 

    calcDialogDisplay = (TextView) findViewById(R.id.tvResult); 

    public void DIVISION(){ 
    number1 = result.get(0); 
    number2 = result.get(1); 

    result.removeAll(result); 

    result.add(number1/number2); 

    calcDialogDisplay.setText(String.format("%.0f", result.get(0))); 
} 
+0

請閱讀'String.format'文檔....你只是得到你要求的 - 小數位0 0 – Selvin 2015-02-11 16:45:38

回答

2

嘗試:

calcDialogDisplay.setText(String.format("%.1f", result.get(0))); // should output 1.7 

更多信息here(科:浮點轉換)。

+0

*應輸出1.6 *不真實... 1.7 – Selvin 2015-02-11 16:57:11

+0

哈哈,是的,我的壞...: d – miselking 2015-02-11 17:01:29