-2

I am trying to create an Android app and I have gotten stuck. I am getting input text with EditText and I need to convert it to a number (as I need to do some mathematical functions). My problem is that none of the integer.something functions works with EditText. Could you suggest something?

Cheers!

3 Answers 3

2

Parsing numbers is pretty easy in Java:

TextView numberView = (TextView)findViewById( ... );
double doubleNumber = Double.parseDouble( numberView.getText().toString() );
float floatNumber = Float.parseFloat( numberView.getText().toString() );
int intNumber = Integer.parseInt( numberView.getText().toString() );

You get the idea. If you want floating point numbers (decimals) then double or float depending on the precision you need. For integer numbers int works.

2

You first need to get the String value from the EditText:

String stringValue = EditText.getText().toString();

and then you can convert it into an int:

int value = Integer.parseInt(stringValue);

and now value will hold the int value of the text in the EditText.

1

You need to get the text from the EditText as a string first:

double d = Double.valueOf(myEditText.getText().toString());

Or

int i = Integer.valueOf(myEditText.getText().toString());

Not the answer you're looking for? Browse other questions tagged or ask your own question.