Android 将十进制后3digits的精度应用于字符串获取运行时异常
我将十进制后的3digits精度应用于字符串,该字符串保存一个浮点值,并在访问另一个edittext时将其设置为edittextAndroid 将十进制后3digits的精度应用于字符串获取运行时异常,android,floating-point,runtimeexception,Android,Floating Point,Runtimeexception,我将十进制后的3digits精度应用于字符串,该字符串保存一个浮点值,并在访问另一个edittext时将其设置为edittext final float f = Float.valueOf(eproductprice.getText().toString()); ebuilding.setOnFocusChangeListener(new OnFocusChangeListener() { @Override public void onFocusChange(View v,
final float f = Float.valueOf(eproductprice.getText().toString());
ebuilding.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus)
{
//Log.e("Float","Message"+f);
String productprice=String.format("%.3f", f);
eproductprice.setText(productprice);
}
}
});
在这里,我想设置格式浮点输入,ebuilding是下一个edittext
我得到了这个错误:
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.omega.waselapp/com.omega.waselapp.Pickupdetails_activity}:
java.lang.NumberFormatException: Invalid float: ""
使用
eproductprice.getText()。
内部onFocusChange
从EditText获取值:
@Override
public void onFocusChange(View v, boolean hasFocus) {
// TODO Auto-generated method stub
if(!hasFocus)
{
// get value here from EditText
float f = Float.valueOf(eproductprice.getText().toString());
String productprice=String.format("%.3f", f);
eproductprice.setText(productprice);
}
}
在调用String.format
之前,请确保字符串不正确,以避免NumberFormatException
或使用try catch处理NumberFormatException