Search code examples
javaandroidnumber-formattingnumberformatexception

How to convert a string with multiple points to double in android


I have a string "3,350,800" with multiple points I want to convert to double but have error multiple points

String number = "3,350,800"
number = number.replace(",", ".");
double value = Double.parseDouble(number);

Error : java.lang.NumberFormatException: multiple points


Solution

  • The . character is used as a decimal point in English, and you cannot have more than one of those in a number.

    It seems like you're using it as a thousands separator though. This is legal in several locales - you just need to use one that allows it, e.g.:

    String number = "3.350.800";
    NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
    double value = format.parse(number).doubleValue();