I got an exception "Unable to parse 53.6 as an integer". What is the proper format?
double dbval;
double temp_val=12;
double p = 1.8;
double fvalue =p;
temp_val = temp_val * fvalue;
temp_val = (temp_val + 32);
String dbcal1=Double.toString(temp_val);
dbval = Integer.parseInt(dbcal1);
System.out.println("dbval"+dbval);
You can't parse 53.6 as an integer because it is not an integer.
If you are just trying to change temp_val to an integer there is no need to convert it to a string and back. You can cast use a cast instead:
int result = (int)temp_val;
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 a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));
I get a double value (eg. -3.1234) which contains a value in decimals (0.1234 in this case). I want to remove the value in decimal value (in this case I wanted the value -3). How do I do it? I know how to remove the decimal if the decimal only has 0, but in this case I don't have the decimal value as 0.
Anyways here is the code for that:
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
Double asdf = 2.0;
Double asdf2 = 2.11;
Double asdf3 = 2000.11;
System.out.println( format.format(asdf) );
System.out.println( format.format(asdf2) );
System.out.println( format.format(asdf3) );
/*
prints-:
2
2.11
2000.11
*/
If you just want to print it :
String val = String.format("%.0f", 2.11)
// val == "2"
If you want to keep the double type but without decimals :
double val = Math.floor(2.11);
// val == 2.000
If you don't care about type you can cast the double into int :
int val = (int) 2.11;
// val == 2
//double as object
int val = myDouble.integerValue();
You can simply use the intValue() method of Double to get the integer part.
You can cast it to int
int i=(int)2.12
System.out.println(i); // Prints 2
Round up decimal value & convert it into integer;
Integer intValue = Integer.valueOf((int) Math.round(doubleValue)));
http://www.studytonight.com/java/type-casting-in-java check out type conversion in java
double asdf2 = 2.11;
int i = (int)asdf2;
System.out.println(asdf2);
System.out.println(i);
Output:
2.11`
2
I have an EditText field where the user can enter his weight in Kilogram, i.e. 11.7. I want to store that in the database as gramms, i.e. 11700.
How do I convert the String "11.7" to the Integer 11700?
To get the value from an EditText use http://developer.android.com/reference/android/widget/EditText.html#getText()
String userInput = editText.getText().toString();
To convert a String into a double use the code from #govindpatel http://developer.android.com/reference/java/lang/Double.html#parseDouble(java.lang.String)
try {
double kg = Double.parseDouble(userInput)
}
catch (NumberFormatException nfe) {
editText.setError(R.string.invalid_double); // Or whatever you want to show the user
}
To convert from KG to g:
long gramms = Math.round(kg * 1000);
You could replace the double and long with float and int if you know you don't need the precision or range of the larger variables.
you have to parse it to int,long or double.
I think In your case you can do this
double grams = Double.parseDouble("11.7") * 1000;
double variable_name = Double.parseDouble(string_name) * number_you_want_to_multiply_with;
and this will give a double value. So you can convert that to integer, long or keep as it is in your db.
to convert double to long you can do something like this:
long value = (long)(grams);
Hope this helps you,
I'm not sure what it has to do with Android?
int grams = (int)(kg * 1000);
To get text from EditText
EditText mEdit = (EditText)findViewById(R.id.edittext);
double kg = Double.parseDouble(mEdit.getText().toString());
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;