开发者

Convert to numbers in Android app [closed]

开发者 https://www.devze.com 2023-03-28 00:53 出处:网络
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical andcannot be reasonably answered in its current form. For help clari
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center. Closed 10 years ago.

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());
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号