我正在从EditText检索一个号码:
long org=0;
String EnteredOrgAmount=amount.getText().toString();
if(!EnteredOrgAmount.equalsIgnoreCase("")) {
org=Long.parseLong(EnteredOrgAmount);
}这很好用,直到我输入7.7,例如I get a numberFormatException。我不能使用逗号,因为我将XML中的EditText声明为:android:inputType="numberDecimal"
有没有办法解决这个问题?
发布于 2013-01-23 20:50:42
不是在长解析中解析,而是在双精度中解析,如果您只需要长值,那么就在双精度中解析并存储在长值中
double org=0;
String EnteredOrgAmount=amount.getText().toString();
if(!EnteredOrgAmount.equalsIgnoreCase("")) {
org=Double.parseDouble(EnteredOrgAmount);
}对于long,只需将org声明为long,而不是双精度
long org=0;
String EnteredOrgAmount=amount.getText().toString();
if(!EnteredOrgAmount.equalsIgnoreCase("")) {
org=(long)Double.parseDouble(EnteredOrgAmount);
}https://stackoverflow.com/questions/14479946
复制相似问题