Converting String to Double in Android

前端 未结 8 663
感情败类
感情败类 2020-12-09 02:29

Trying to get double values from an EditText and manipulate them before passing them to another Intent. Not using primitive data type so I can use toString methods.

8条回答
  •  暖寄归人
    2020-12-09 03:00

    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.

提交回复
热议问题