Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String from EditText to float

Tags:

java

android

I have an EditText for which will be using for a float number. So I'm trying to read the text from the EditText and put it into a float variable. But I seem to have a text to float problem. This is the line I have:

float Number = ( ( EditText )findViewById( R.id.edit_float ) ).getText();

I've tried using Float.parseFloat(string) and just general casting, but nothing seem to do it. What can I do here? Also, is there a way to check for a valid float number before writing it to a variable?

like image 247
Espen Avatar asked Nov 19 '10 21:11

Espen


1 Answers

Try this.

EditText edt = (EditText) findViewById(R.id.edit_float);
float number = Float.valueOf(edt.getText().toString());

You use the valueOf() method if the Float wrapper class to convert a string to a float. IN this example I get the Editable object of that EditText with getText() on which I call the toString() method to obtain a string from it.

Update: Totally right guys sorry. Time to increment my sheep counter.

like image 164
Octavian A. Damiean Avatar answered Nov 14 '22 22:11

Octavian A. Damiean