问题
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