我写了用于学习Java的android计算器,但我在应用程序中出错。如果我输入5/3-答案是2,但我需要浮点数(1,6)。我需要改变什么?
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)));
}
最佳答案
尝试:
calcDialogDisplay.setText(String.format("%.1f", result.get(0))); // should output 1.7
更多信息here(部分:浮点转换)。