2

我在这行代码中得到一个 NumberFormatException : invalid double "111,000,000" :

double SalePotential = Double.valueOf(EtPotential.getText().toString());

一开始我使用 Numberformat 来格式化我的双精度值以分隔数字并将其插入到 EditText 但是当我尝试检索 EditText 的值时,它会抛出异常:

NumberFormat f = NumberFormat.getInstance();
EtPotential.setText(String.valueOf(f.format(PTData.SalePotential)));

我也试过 DecimalFormat 或 Double.parseDouble 没有成功。任何帮助,将不胜感激!:

DecimalFormat f = new DecimalFormat("###.###", DecimalFormatSymbols.getInstance());
double SalePotential = Double.parseDouble(EtPotential.getText().toString());
4

2 回答 2

8

解析前去掉“,”

double SalePotential = Double.parseDouble(EtPotential.getText().toString().replace(",", ""));

更新:正确实施

double salePotential = 0; // Variable name should start with small letter 
try {
    salePotential = Double.parseDouble(EtPotential.getText().toString().replace(",", ""));
} catch (NumberFormatException e) {
    // EditText EtPotential does not contain a valid double
}

快乐编码:)

于 2013-05-08T06:59:27.183 回答
1

使用替换功能替换所有出现的 ,

     String  s= EtPotential.getText().toString();
     s = s.replace(",", ""); 
     try
     {
       double SalePotential = Double.parseDouble(s);
     }catch(NumberFormatException e)
     {
         e.printStackTrace();
     }
于 2013-05-08T07:02:38.340 回答