为我的 android 编写一个计算工具。输入之一是以英尺和英寸为单位的距离。
我有两个输入(input3 和 input4),分别用于英尺和英寸。在我的计算中,我试图将这两个输入转换为十进制数,以用于等式的其余部分。这是我执行此操作的代码部分:
private void doCalculation() {
// Get entered input value
String strValue3 = input3.getText().toString();
String strValue4 = input4.getText().toString();
// Perform a hard-coded calculation
double imperial1 = (Integer.parseInt(strValue3) + (Integer.parseInt(strValue4) / 12));
// Update the UI with the result to test if calc worked
output2.setText("Test: "+ imperial1);
}
我的测试值是 4 英尺 6 英寸。 4 看起来很好,但是 6 英寸除以 12 时默认为 0。所以我的结果是 4.0 我尝试将计算减少到仅除法运算,结果是 0.0
我做错了什么? (仅供引用:这是我第一次使用 Java)
最佳答案
你的类型是错误的。当它们确实应该是 double 时,您将它们解析为整数。
尝试:
double imperial1 = Double.parseDouble(strValue3) +
(Double.parseDouble(strValue4) / 12.0);
关于java - 基本英制转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2128245/