Java Android : result in the calculator displays integers(need float/double)

标签 java android int calculator

我写了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/

相关文章:

java - 在不同的窗口中查看

java - FileInboundAdapter - 在运行时设置新触发器

java - Spring批处理如何使一个步骤失败并因此从分区器内部执行作业

java - 在应用程序关闭后保持广播接收器运行

Android-将日期和文本保存到手机内存中的 SQLite?

android - 从 .json 文件创建下拉菜单

c# - Xamarin Forms 元素父属性为空

C 程序未在 Objective-C 编译器上正确编译

python - 从字符串转换为 int python 的麻烦

java - 返回整数的第一个数字