Invalid double in converting String to Double

前端 未结 2 1563
孤城傲影
孤城傲影 2021-01-26 06:05

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

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


        
相关标签:
2条回答
  • 2021-01-26 06:29

    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();
         }
    
    0 讨论(0)
  • 2021-01-26 06:30

    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 :)

    0 讨论(0)
提交回复
热议问题