我写了android计算器来学习java,但我的应用程序出现错误。如果我输入 5/3 - 答案是 2,但我需要 float 结果 (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 (部分:浮点转换)。
关于Java Android : result in the calculator displays integers(need float/double),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28459640/