Converting String to Double in Android Converting String to Double in Android android android

Converting String to Double in Android


I would do it this way:

try {  txtProt = (EditText) findViewById(R.id.Protein); // Same  p = txtProt.getText().toString(); // Same  protein = Double.parseDouble(p); // Make use of autoboxing.  It's also easier to read.} catch (NumberFormatException e) {  // p did not contain a valid double}

EDIT: "the program force closes immediately without leaving any info in the logcat"

I don't know bout not leaving information in the logcat output, but a force-close generally means there's an uncaught exception - like a NumberFormatException.


try this:

double d= Double.parseDouble(yourString);


You seem to assign Double object into native double value field. Does that really compile?

Double.valueOf() creates a Double object so .doubleValue() should not be necessary.

If you want native double field, you need to define the field as double and then use .doubleValue()