16

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?

Espen
  • 3,607
  • 11
  • 48
  • 75
  • Make sure you get in the habit of looking at return types. Obviously trying to set a string into a float won't work. You have to convert it as seen below in Octavian Damiean's answer. – Thomas Nov 19 '10 at 22:05
  • On that topic as well, he's actually trying to cast from an Editable to a float with this line, as `getText()` returns an Editable. – Kevin Coppock Nov 19 '10 at 22:08

3 Answers3

39

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.

Octavian Helm
  • 39,405
  • 19
  • 98
  • 102
  • 2
    `getText()` returns an `Editable`. You'll need to use `edt.getText().toString();`. edit: beaten by 1 min. :P – Kevin Coppock Nov 19 '10 at 22:06
  • Thanx! Now my full line is: Float.valueOf( ( ( EditText )findViewById( R.id.edittext_wireframe_width ) ).getText().toString() ); – Espen Nov 19 '10 at 22:08
  • 3
    On that note, I wonder why Google chose to do it that way? I would think separate `Editable getEditable()` and `String getText()` methods would be clearer, wouldn't they? – Kevin Coppock Nov 19 '10 at 22:11
  • 1
    @kcoppock: The object referenced by an `EditText` instance is an `Editable`; returning a `String` would mean creating an instance every time that `getText` is called. Such an API makes sure that the creation of the `String` instance is explicit. (for more details about why creating too many objects is bad, see http://developer.android.com/guide/practices/design/performance.html#object_creation) – Jean Hominal Nov 19 '10 at 22:35
  • @kcoppock: It's not an info, it is just speculation from my part - should have said so more clearly in the comment. – Jean Hominal Nov 20 '10 at 07:45
0
float getFloatFrom(EditText txt) {
    try {
        return NumberFormat.getInstance().parse(txt.getText().toString()).floatValue();
    } catch (ParseException e) {
        return 0.0f;
    }
}

Do NOT use Float.valueOf() if you want the code to work in countries that use decimal comma instead of decimal point.

Daniel
  • 26
  • 1
  • I believe that number conversion is best handled as per [how-to-parse-number-string-containing-commas-into-an-integer-in-java](https://stackoverflow.com/questions/11973383/how-to-parse-number-string-containing-commas-into-an-integer-in-java) – bated Sep 28 '17 at 19:56
  • Can you be more specific? – Daniel Sep 30 '17 at 07:45
0

String stext = editText.getText().toString();

float text = Float.parseFloat(stext);

  • 2
    While this code may solve the question, [including an explanation](//meta.stackexchange.com/q/114762) of how and why this solves the problem would really help to improve the quality of your post, and probably result in more up-votes. Remember that you are answering the question for readers in the future, not just the person asking now. Please [edit] your answer to add explanations and give an indication of what limitations and assumptions apply. – Yunnosch Jul 11 '22 at 18:00