Convert to numbers in Android app [closed]
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 fun开发者_StackOverflowctions works with EditText. Could you suggest something?
Cheers!
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.
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
.
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());
精彩评论