I want to make the text inside the EditText converted to float
anyone know how to cast from EditText.getText() to float?
I make this solution
float salary= new Float(salaryET.getText().toString());
and it work ok
thanks for every body
Float f = Float.valueOf(EditText.getText().toString());
this code to convert EditText text to float
String str = EditText.getText().toString;
try{
Float f = new Float(str);
System.out.println("Float value is: = " + f);
}catch (Exception e){
System.out.println("Exception: " + e.getMessage());
}
use the above code to convert editable to float. Also added the try catch block for any exceptions while converting.
Updated:::
Float f = Float.parseFloat(str);//you can also use this
Related
I've been trying to convert the following string to double value from my Shared Preferences key but still failed even I've tried to follow the solution from the previous Q&A (Android - SharedPreference converting to Double)
Here are my code:
String strCurr_Lat = FM_SharePrefs.getString("FM_Curr_Lat", "");
String strCurr_Lng = FM_SharePrefs.getString("FM_Curr_Lng", "");
String strDest_Lat = FM_SharePrefs.getString("FM_Dest_Lat", "");
String strDest_Lng = FM_SharePrefs.getString("FM_Dest_Lng", "");
Double dCurr_Lat = Double.parseDouble(strCurr_Lat);
Failed and throw an error when reach the assign double variable.
Can anyone assist? Thank you very much.
-sea-
Try this :)
Double dCurr_Lat = Double.valueOf(strCurr_Lat);
To parse in double your string need to in floating number formate. So first you have to check whether it is or not , Otherwise it will throw NumberFormatException .
String strCurr_Lat = FM_SharePrefs.getString("FM_Curr_Lat", "");
if(!TextUtils.isEmpty(strCurr_Lat)){
try {
Double curr_Lat = Double.parseDouble(strCurr_Lat);
}catch (NumberFormatException e){
e.printStackTrace();
Log.e("Parse","String not in floating format");
}
}
if your value is not null then try this it will work.
double dCurr_Lat= Double.parseDouble(strCurr_Lat);
I have two editText. First edit text is amount, second edit text is description. I get the amunt edit text values float, description edit text values string. But error when control values. I think wrong "(tutarEdit.getText().toString().equals("")".
Thanks in Advance..
final EditText tutarEdit = (EditText) layout.findViewById(R.id.editTextTutar);
final EditText aciklamaEdit = (EditText) layout.findViewById(R.id.editTextAciklama);
Float tutar = Float.parseFloat(tutarEdit.getText().toString());
String aciklama = aciklamaEdit.getText().toString();
if(tutarEdit.getText().toString().equals("") || aciklamaEdit.getText().toString().equals("")){
Toast.makeText(MainActivity.this, "Void", Toast.LENGTH_LONG).show();
}
You are getting parse error when tutarEdit is "", surround it with try/catch
Float tutar = 0;
try {
tutar = Float.parseFloat(tutarEdit.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
How can I display special characters(like – ") in a TextView?
You can use the Html.fromHtml() to handle HTML formatted text into a Spannable that TextView can display.
If you know the Unicode value, you can display any UTF-8 character. Example, for " you would have &\#0034;.
See Unicode Characters (at Code Table) for more information.
I've implemented this solution.
Activity class:
textView.setText( getString(R.string.author_quote, "To be or not to be", "Shakespeare") )
strings.xml:
<string name="author_quote">« %1$s » - %2$s</string>
HTML chars are written directly in strings.xml, no additional Html.fromHtml() is needed. It works fine on all my devices.
I've written a custom method that will convert all unicode from hexa to integer and replaces from actual string.
So that text view can read is as a unicode.
have a look ,this will solve your problem...
public String unecodeStr(String escapedString) {
try {
String str;
int from = 0;
int index = escapedString.indexOf("\\u", 0);
while (index > -1) {
str = escapedString.substring(index, index + 6).replace("\\u", "");
try {
Integer iI = Integer.parseInt(str, 16);
char[] chaCha = Character.toChars(iI);
escapedString = escapedString.replaceFirst(str, String.valueOf(chaCha));
} catch (Exception e) {
CustomLog.e("error:", e.getMessage());
}
from = index + 3;
index = escapedString.indexOf("\\u", from);
}
escapedString = escapedString.replace("\\u", "");
} catch (Exception e) {
CustomLog.info("warnning", "emoji parsing error at " + escapedString);
}
return escapedString;
}
Is there way to convert a string to double. This is how i did.
String s = b.getText().toString();
double d = Double.parseDouble(s);
It gives NumberFormatException
Try this
String s = b.getText().toString();
double d = Double.valueOf(s.trim()).doubleValue();
You are probably starting off with a zero-length string. Check the length before you parse it. It also wouldn't hurt to catch NumberFormatException, although that shouldn't be problem if you have android:inputType="number" in the view's layout.
double d = 0;
CharSequence cs = b.getText();
if(cs != null && cs.length() > 0) {
d = Double.parseDouble(cs.toString());
}
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;
I have this code to control if a EditTextPreference is null or not:
case R.id.prochain_vidange:
settings = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String choix_huile = settings.getString("listPref_huile_moteur", "0");
km = settings.getString("km", "");
Log.d("TAG",km);
int x= Integer.valueOf(km);
if (km != "")
{
if (Integer.valueOf(choix_huile) == 0) {
............
The problem is in this line:
int x= Integer.valueOf(km);
What could be the problem ?
Thanks.
If you give Integer.valueOf(String s) a string that is not a valid number, it throws a NumberFormatException. Change the default value to 0:
km = settings.getString("km", "0");
Alternatively, you can catch the exception, and set x to 0:
km = settings.getString("km", "");
int x;
try {
x = Integer.valueOf(km);
} catch(NumberFormatException e) {
x = 0;
}
Integer.valueOf trys to make a new Integer with .parseInteger(String s), "" cant be parsed to a valid number so you get a NumberFormatException
You can catch it with a try catch block or you can simply dont try to make a Integer with the String "".
before:
int x= Integer.valueOf(km);
if (km != "") {
after:
if (km != "") {
int x= Integer.valueOf(km);
Integer.valueOf(km) can throw an exception if the the km string is not able to be parsed as an integer.
However, wrapping it in a try { } catch() block is not an approach I would recommend.
The whole purpose of having a default value on the getString() method in SharedPreferences is that there can be a default value to fall back on if the preference doesn't exist. So the better way to solve this is to modify your settings.getString(...) call to be like this:
km = settings.getString("km", "0");
Then your subsequent call to Integer.valueOf(km) will not have a blank to fail upon.
Is the input string coming from a blank text field where the user can enter any value? If so, it's at that point that you can validate the value that the user entered. By validating the input early on, you won't need to scatter the checking/validating mechanism to other areas of your code.