24

Possible Duplicate:
String from EditText to float

In application I want to convert the entered string in edit box to the corresponding value like 233243664376347845.89 to corresponding float value. But it returns like IE10 after some number for example 23324366IE10 Please help me. My code is -

NumberFormat format = NumberFormat.getInstance(Locale.US);

try 
{
    number = format.parse(e1.getText().toString());
} catch (ParseException e) 
{
    // TODO Auto-generated catch block
    e.printStackTrace();
}

The edit text length is greater then 20 digits,also i want to minus two edit text float values...

Community
  • 1
  • 1
Sathya
  • 263
  • 1
  • 2
  • 7
  • use Float.valueOf(String) : http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Float.html#valueOf%28java.lang.String%29 – Blackbelt May 24 '12 at 10:32
  • 1
    The `double` value (it's not a `float`) is parsed just fine. The problem is that it's not *displayed* right. Where's the code that prints `number`? – Jon May 24 '12 at 10:33
  • Float f =Float.parseFloat(str); – MAC May 24 '12 at 10:34

3 Answers3

55
String s = e1.getText().toString();
Float f= Float.parseFloat(s);
Parag Chauhan
  • 35,760
  • 13
  • 86
  • 95
18

This will solve your issue:

String str=e1.getText().toString();
number = Float.parseFloat(str);
prolink007
  • 33,872
  • 24
  • 117
  • 185
user1208720
  • 515
  • 2
  • 6
0

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.

Ponmalar
  • 6,871
  • 10
  • 50
  • 80