How can I convert the Currency to float? for example € 10,10 to 10.10

I tried it but no good.

float f = Float.valueOf(tv.getText().toString()).floatValue();
有帮助吗?

解决方案

String temp = tv.getText().toString().replace.(",","."); tv.setText(""+temp);

其他提示

Of course it's no good, because you have to remove the character of the currency first (€, $...). You could use String.replace("€", "") for example, and then Float.parseFloat(yourString).

One suggestion, do not allow the user to type in any character but numbers so parsing does not fail

In the EditText for input

android:inputType="numberDecimal"

http://developer.android.com/reference/android/widget/TextView.html#attr_android:inputType

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top