Java Android:计算器中的结果显示整数(需要浮点/双精度)
我为学习java编写了android计算器,但在应用程序中出现了一个错误。若我输入5/3-答案是2,但我需要浮动结果(1,6)。我需要改变什么Java Android:计算器中的结果显示整数(需要浮点/双精度),java,android,int,calculator,Java,Android,Int,Calculator,我为学习java编写了android计算器,但在应用程序中出现了一个错误。若我输入5/3-答案是2,但我需要浮动结果(1,6)。我需要改变什么 ArrayList<Float> result = new ArrayList<Float>(); float number1; float number2; calcDialogDisplay = (TextView) findViewById(R.id.tvResult); pub
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)));
}
ArrayList结果=新建ArrayList();
浮点数1;
浮点数2;
calcDialogDisplay=(TextView)findViewById(R.id.tvResult);
公共事务部(){
number1=结果。获取(0);
number2=结果。获取(1);
result.removeAll(result);
结果。添加(编号1/2);
calcDialogDisplay.setText(String.format(“%.0f”,result.get(0));
}
试试:
更多信息(部分:浮点转换)。请阅读
String.format
文档。。。。你只是得到了你想要的-0小数位输出1.6不是真的。。。1.7
calcDialogDisplay.setText(String.format("%.1f", result.get(0))); // should output 1.7