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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With