Search code examples
javaandroidnumberformatexception

NumberFormatException for input string to double


In my android app, I am trying to convert a string value to a double value. But it throws NumberFormatException. The string value is "30.192781000000000"

My code is,

  String Latitude="30.192781000000000";
  double Lat2 = Double.parseDouble(Latitude.trim());

I have tried with this also,

  NumberFormat nf = NumberFormat.getInstance(Locale.US); // Looks like a US format
  double lat2=nf.parse(Latitude.trim()).toDouble();

But didnot work. Please suggest me any solution. I have to work with a double value and not a string value.

Thanks in advance!!


Solution

  • Try this Code i think this should work.

    NumberFormat _format = NumberFormat.getInstance(Locale.US);
        Number number = null;
        try {
            number = _format.parse(latitude);
            double _double = Double.parseDouble(number.toString());
            System.err.println("Double Value is :"+_double);
        } catch (ParseException e) {
    
        }
    

    Thank you,