Search code examples
javaandroiddoublenumberformatexceptiondecimalformat

Double.parseDouble() Exception


I have this DecimalFormat:

DecimalFormat formatSci = new DecimalFormat("#.#########E0");

When I have

text.append(formatSci.format(answer));//answer is a double value

and after that:

double b = Double.parseDouble(text.toString());//text is something like 2.3333333E10

It throws NumberFormatException. What is wrong with this as apparently it works on android 4.4.4 and above and not on older versions?


Solution

  • This may happen because of your internal Locale specification.

    Try using:

    NumberFormat format = NumberFormat.getInstance(Locale.US);
    Number number = format.parse(text.toString());
    double d = number.doubleValue();
    // OR
    double d = Double.parseDouble(text.toString());