Invalid double in converting String to Double

天大地大妈咪最大 提交于 2019-12-02 17:15:04

问题


i get a NumberFormatException : invalid double "111,000,000" in this line of code :

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

in the beginning i've used Numberformat , to format my double value for separating number and inserted it to an EditText but when i try to retrieve the value of EditText it throws me the exception :

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

i've also tried DecimalFormat or Double.parseDouble with no Success. any help would be Appreciated! :

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

回答1:


Remove "," before parsing

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

Update : With proper implementation

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
}

Happy coding :)




回答2:


Use replace function to replace all occurences of ,

     String  s= EtPotential.getText().toString();
     s = s.replace(",", ""); 
     try
     {
       double SalePotential = Double.parseDouble(s);
     }catch(NumberFormatException e)
     {
         e.printStackTrace();
     }


来源:https://stackoverflow.com/questions/16434386/invalid-double-in-converting-string-to-double

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!