我编写了一种方法,通过单击切换按钮将高度从厘米转换为英尺英寸,但是当我在输入以厘米为单位的高度后尝试单击切换按钮时,它抛出了我的错误 java.lang.NumberFormatException:无效的 int:“” 这是我的代码:
private void convertTofeetInches(EditText height_cm){
String str = height_cm.getText().toString();
int feet = (int) Math.floor(Integer.parseInt(str)/30.48);
int inches = (int)Math.round((Double.parseDouble(str)/2.54) - ((int)feet * 12));
Log.d("feet",String.valueOf(feet));
Log.d("inches",String.valueOf(inches));
enter_height.setText(""+feet + "'" +inches + "\"");
}
我认为我在类型转换方面犯了一些错误。谁能指点一下。
最佳答案
试试这个,
private void convertTofeetInches(String str) throws NumberFormatException{
Double value = new Double(str);
int feet = (int) Math.floor(value / 30.48);
int inches = (int) Math.round((value / 2.54) - ((int) feet * 12));
String ouput = feet + "' " + inches + "\"";
enter_height.setText(ouput);
}
关于android - 将高度从厘米转换为英尺英寸时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39425026/