如何在android [duplicate]中将字符串转换为浮点值

时间:2021-11-17 20:55:04

Possible Duplicate:
String from EditText to float

可能重复:从EditText到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 -

在应用程序中,我想将编辑框中输入的字符串转换为相应的值,如233243664376347845.89到相应的浮点值。但它返回像IE10后的一些数字,例如23324366IE10请帮助我。我的代码是 -

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...

编辑文本长度大于20位,我也想减去两个编辑文本浮点值...

3 个解决方案

#1


43  

String s = e1.getText().toString();
Float f= Float.parseFloat(s);

#2


16  

This will solve your issue:

这将解决您的问题:

String str=e1.getText().toString();
number = Float.parseFloat(str);

#3


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.

如果Float包装类将字符串转换为float,则使用valueOf()方法。在这个例子中,我使用getText()获取EditText的Editable对象,我在其上调用toString()方法从中获取字符串。

#1


43  

String s = e1.getText().toString();
Float f= Float.parseFloat(s);

#2


16  

This will solve your issue:

这将解决您的问题:

String str=e1.getText().toString();
number = Float.parseFloat(str);

#3


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.

如果Float包装类将字符串转换为float,则使用valueOf()方法。在这个例子中,我使用getText()获取EditText的Editable对象,我在其上调用toString()方法从中获取字符串。