public static int convertCelsiusToFahrenheit(Long celcious){
return Math.round(celcious * 9/5 + 32);
}
我正在尝试将其设置为我的TextField。我无法在Text Field
中设置值,因为我得到了NumberFormatException。 如何使用精确值发送它。
holder.textItem.setText(convertCelsiusToFahrenheit(Long.parseLong(
custom.getTemperature())));
java.lang.NumberFormatException:无效长:“32.2222222222222”
答案 0 :(得分:2)
将float
转换为long
你可以使用一个演员
(long) custom.getTemperature()
但是,如果您拥有的是表示浮点数的String,则可能需要更改转换函数,以接收浮点数.-
public static int convertCelsiusToFahrenheit(float celcious){
return Math.round(celcious * 9.0f / 5.0f + 32);
}
传递
Float.parseFloat(custom.getTemperature())
不要忘记除9.0f / 5.0f
而不是9 / 5
,因为最后一个是整数除法,并且总是返回1.
答案 1 :(得分:2)
试试这个,
holder.textItem.setText(" "+convertCelsiusToFahrenheit(Float.parseFloat(
custom.getTemperature())));
和
public static int convertCelsiusToFahrenheit(Float celcious){
return Math.round(celcious * 9/5 + 32);
}
答案 2 :(得分:1)
除了解析为float
等的答案之外,请注意setText
已超载。当它需要一个int时,它是一个资源ID,所以创建一个string
并给它:
而不是:
holder.textItem.setText(convertCelsiusToFahrenheit(...));
执行:
holder.textItem.setText(String.format("%d", convertCelsiusToFahrenheit(...)));
或者:
holder.textItem.setText(String.format("%d\x00B0 f", convertCelsiusToFahrenheit(...)));
应该给你“10°f”(未经测试)