askIT

Android Convert string to double

If you have a string that contains a number with decimal you can make it into a double. This can be useful if you get information a text field that you will use for a calculation.

String to double

String myDoubleString = "3.1415";
double myDouble = Double.parseDouble(myDoubleString);

Example of retrieving text from a text field:

EditText myText = (EditText) findViewById(R.id.entry);
String myString = myText.getText().toString();

double myDouble = 0;
try {
        myDouble = Double.parseDouble(myString);
}
catch(NumberFormatException nfe) {
        System.out.println("Could not parse " + nfe);
}


Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.